期望一个类型,得到一个模板

Expected a type, got a template

本文关键字:一个 期望 类型      更新时间:2023-10-16

我正在编写一种"异步工厂",其中耗时的具体对象构造被推迟到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指针调用不同的makers。但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_ptrstd::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

结构可能应该位于单独的命名空间中,并且可能具有更好的名称。留给读者练习:)