期望一个类型,得到一个模板
Expected a type, got a template
我正在编写一种"异步工厂",其中耗时的具体对象构造被推迟到std::async
任务。 每个AsyncFactory
都将存储指向对象的智能指针。
[这不是工厂模式最正确的应用,但它是为了 MWE]。
#include <future>
#include <memory>
#include <type_traits>
#include <cassert>
/**
* @param I is the interface type
* @param Ptr is the memory handler. Default = unique; optional = shared
*/
template <class I, template<class> class Ptr = std::unique_ptr>
class AsyncFactory
{
/**
* @param C - the concrete type for the interface I
* @param Ts - the variadic params
*/
template <class C, typename... Ts>
void _future_reload(Ts&&... params)
{
if (std::is_same<Ptr, std::unique_ptr>()) // line21
{
ptr = std::make_unique<C>(std::forward<Ts>(params)...);
}
else
{
if (std::is_same<Ptr, std::shared_ptr>()) // line27
{
ptr = std::make_shared<C>(std::forward<Ts>(params)...);
}
else
{
static_assert(0, "unacceptable type for smart pointer");// line33
}
}
}
public:
Ptr<I> ptr;
AsyncFactory() :
ptr(nullptr)
{}
/**
* @param C - the concrete type. Default: the interface type
* @param Ts - the variadic params
*/
template <class C = I, typename... Ts>
void reload(Ts&&... params)
{
std::future<void> fReload =
std::async(std::launch::async,
&AsyncFactory::_future_reload<C, Ts...>, this,
std::forward<Ts>(params)...);
}
};
class BaseVirtual
{
virtual void foo() = 0;
};
class DerivedConcrete :
public BaseVirtual
{
void foo() override {;}
};
int main()
{
AsyncFactory<BaseVirtual, std::shared_ptr> fac;
fac.reload<DerivedConcrete>();
}
智能指针出现问题。我必须为unique
/shared
指针调用不同的make
rs。但g++ -std=c++14
止步于
f.cpp: In member function ‘void AsyncFactory<I, Ptr>::_future_reload(Ts&& ...)’:
f.cpp:21:44: error: type/value mismatch at argument 1 in template parameter list for ‘template<class, class> struct std::is_same’
if (std::is_same<Ptr, std::unique_ptr>())
^
f.cpp:21:44: note: expected a type, got ‘Ptr’
f.cpp:21:44: error: type/value mismatch at argument 2 in template parameter list for ‘template<class, class> struct std::is_same’
f.cpp:21:44: note: expected a type, got ‘unique_ptr’
f.cpp:27:45: error: type/value mismatch at argument 1 in template parameter list for ‘template<class, class> struct std::is_same’
if (std::is_same<Ptr, std::shared_ptr>())
^
f.cpp:27:45: note: expected a type, got ‘Ptr’
f.cpp:27:45: error: type/value mismatch at argument 2 in template parameter list for ‘template<class, class> struct std::is_same’
f.cpp:27:45: note: expected a type, got ‘shared_ptr’
f.cpp:33:9: error: static assertion failed: unacceptable type for smart pointer
static_assert(0, "unacceptable type for smart pointer");
std::unique_ptr
不是一种类型。std::unique_ptr<int>
是一种类型。您需要传递显式模板参数才能在is_same
中使用它。
此外,您可能不想以这种方式使用if
,因为无论is_same
的结果如何,这两个分支都需要有效。在 C++17 中,您将使用if constexpr(...)
来解决此问题 - 在 C++14 中,您可以使用更传统的基于重载的方法或基于标记调度的方法。例如
auto impl(std::true_type /* is shared ptr */) { /* ... */ }
auto impl(std::false_type /* is unique ptr */) { /* ... */ }
用法:
ptr = impl(std::is_same<Ptr, std::shared_ptr<T>>{}, /* ... */);
您不能像这样比较模板模板参数,只能将具体类型与std::is_same
进行比较。此外,您甚至不能再对std::shared_ptr
和std::unique_ptr
使用相同的模板签名AsyncFactory
:https://godbolt.org/g/tNiYB1
问题的解决方案是引入另一层抽象:
struct UseUnique
{
template<class I>
using Ptr = std::unique_ptr<I>;
template<class C, typename... Ts>
static auto build(Ts&&... params)
{
return std::make_unique<C>(std::forward<Ts>(params)...);
}
};
struct UseShared
{
template<class I>
using Ptr = std::shared_ptr<I>;
template<class C, typename... Ts>
static auto build(Ts&&... params)
{
return std::make_shared<C>(std::forward<Ts>(params)...);
}
};
这些结构包含定义成员和构建具体类型(即要使用的指针类型和make_X
函数(所需的信息。然后你可以做:
template <class I, class UseWhat = UseShared>
class AsyncFactory
{
template <class C, typename... Ts>
void _future_reload(Ts&&... params)
{
ptr = UseWhat::template build<C>(std::forward<Ts>(params)...);
}
public:
using Ptr = typename UseWhat::template Ptr<I>;
Ptr ptr;
// ...
}
因此
AsyncFactory<Interf, UseUnique> fac;
fac.reload<Concrete>();
在此处完成工作代码:https://godbolt.org/g/L2511J
结构可能应该位于单独的命名空间中,并且可能具有更好的名称。留给读者练习:)
相关文章:
- 一元*运算符的操作数是否期望一个 prvalue
- 什么 /可以/ 你从一个函数返回. 并期望它在到达时还活着?
- 期望在奇怪的地方有一个";"
- 为什么我可以将 std::map 的键传递给一个期望非常量的函数?
- 期望一个类型,得到一个模板
- 尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
- Google Mock Expect_call将退休另一个期望
- 我一直得到一个期望;错误,我不确定为什么
- C++:当我输入一个字符串时,由于某种原因,程序期望更多,并导致整个代码出现问题
- 使用 gtest EXPECT_CALL 时竞争条件段错误,而另一个期望是执行相同的方法
- 如何初始化一个在C++中期望向量和变量的构造函数<int>?
- 使用谷歌测试期望一个给定范围内的值
- 我有 3 个错误:期望一个")",期望一个表达式,long 类型的参数与 U32 类型的参数不兼容
- GoogleMock:如何准确地期望一个带有特定参数的调用,并查看故障诊断
- 我可以复制一个谷歌模拟对象后设置期望
- 我可以在共享内存中放置一个 std::atomic 并<int64>期望原子操作吗?
- 期望头文件中有一个参数
- 如何为一个期望istream的方法定义原型
- 为什么Pascal Script期望在外部函数声明附近再加一个分号
- 如何启动一个std::线程,调用一个期望函数作为参数的函数