小编web*_*nra的帖子

如何比较模板模板和模板实例?

首先,让我向您介绍一个部分解决方案:

template <template <class...> class,
        typename ...>
struct is_tbase_of:
  std::false_type
{ };

template <template <class...> class Type,
          typename ...Args>
struct is_tbase_of<Type, Type<Args...>>:
  std::true_type
{ };
Run Code Online (Sandbox Code Playgroud)

在一般情况下,它的工作原理:

is_tbase_of<std::vector, std::is_integral<int>>::value; // false
is_tbase_of<std::vector, std::vector<int>>::value;      // true
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于"元返回"模板模板,例如:

template <template <class ...> class T>
struct quote
{
  template <typename ...U>
  using type = T<U...>;
};

using QVec =  quote<std::vector>;
is_tbase_of<QVec::template type, std::vector<int>>::value; // false...
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,尝试获取第二个类型的模板参数(比较引用的类型特化)但似乎我无法让它们工作.即使专注is_tbase_ofquote(这将是一个不太通用但足够的选项)似乎将我发送到模板模式匹配的黑角.

c++ templates metaprogramming c++11 c++14

11
推荐指数
1
解决办法
338
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

metaprogramming ×1

templates ×1