我想专门化以下模板类的类型参数,它有一个类型参数和一个模板模板参数:
template <
typename T,
template <typename E> class Foo
> class Bar;
我尝试了添加和/或省略 .template
的所有排列组合和 typename
在以下每个片段的最后一行,都没有编译:
1.)
template <
template <typename E> class Foo
> class Bar<int, Foo<typename E>>;
2.)
template <
template <typename E> class Foo
> class Bar<int, Foo.template <typename E>>;
3.)
template <
template <typename E> class Foo
> class Bar<int, Foo<E>>;
4.)
template <
template <typename E> class Foo
class Bar<int, Foo.template <E>>;
为什么它们都不起作用?
关于每个适用片段的最后一行:
- 不是
typename
澄清E
是类Foo
使用的类型, 或者此语法只能在{}
中使用Bar
的正文的类定义? - 不是
template
澄清Foo
是一个模板,因此阻止编译器解析Foo <
作为Foo
“小于”,或者此语法只能在{}
中使用Bar
的正文的类定义?
我怎样才能让它工作?
最佳答案
Doesn't
typename
clarifyE
is a type used by classFoo
, or can this syntax only be used within the{}
body ofBar
's class definition?
typename
仅在您在模板定义中定义类型时使用(class
也可以使用)或当您访问依赖类型(一种类型取决于模板参数)。
有关更多信息(甚至关于何时使用 template
),请参阅 this thread .
How can I get this to work?
模板模板参数中类型的名称实际上不能使用。这只是一种形式。您必须向主模板添加另一个模板参数:
template <
template<typename> class Foo,
typename E
> class Bar<int, Foo<E>> { ... };
此外,如果这是模板 Bar
的特化,则 Bar
需要一个主模板来特化:
template<typename T, typename U>
struct Bar;
template <
template<typename> class Foo,
typename E
> class Bar<int, Foo<E>> { ... };
关于c++ - 具有类型和模板模板参数的模板类中类型参数的部分特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860571/