【问题标题】:How to use a template template argument from a member templated type alias of a struct如何使用结构的成员模板化类型别名中的模板模板参数
【发布时间】:2021-11-21 18:07:19
【问题描述】:

我正在尝试使用来自结构的成员类型别名的模板模板参数,但我找不到正确的语法:

struct A
{
   template <typename T>
   using type = int;
};

template <template <typename> class C>
struct B
{
   // ...
};

B<typename A::type> b; // Does not compile: error: template argument for template template parameter must be a class template or type alias template
B<typename A::template type> b; // Does not compile: error: expected an identifier or template-id after '::'
B<typename A::template <typename> type> b; // Does not compile
B<typename A::template <typename> class type> b; // Does not compile

【问题讨论】:

  • 试试B&lt;A::type&gt; b;
  • @JeJo 我想我可以使用不同的设计,这是已知的 c++ 限制吗?
  • @cigien 这也会导致error: template argument for template template parameter must be a class template or type alias template
  • 好像ok。您使用的是什么编译器、版本和标志?
  • @cigien 我正在使用Apple clang version 11.0.0 (clang-1100.0.33.16) 虽然,如下所述B&lt;A::template type&gt; b 正在为我工​​作。

标签: c++ c++11 templates


【解决方案1】:

A::type 是一个模板,而不是一个类型名。

B<A::template type> b1;  // OK
B<A::type> b2;           // OK

【讨论】:

  • 我不确定为什么,但就我而言,只有第一个命题有效 (b1)。无论如何,非常感谢!
  • @user2237143 可能A的真实代码有点不同。
  • 你是对的,在我的例子中A 本身就是一个模板类。这有什么不同吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
相关资源
最近更新 更多