【发布时间】:2014-06-21 16:47:21
【问题描述】:
是否可以在模板特化中使用类模板作为参数?
我希望能够使用foo<Foo<int>>() 之类的东西(请参阅源代码中的#3)并为该模板实例运行具有唯一代码。目前只有普通的专业化工作(见#2)。
以前的similar question 会让我相信#3 的方法会起作用,但代码至少在 msvc2012 下不起作用。
我正在尝试做的事情可能吗?如果有,怎么做?
来源
// Test struct.
template<class T>
struct Foo
{
T foo;
};
// #1 Ordinary template
template<class T>
T foo()
{
return T();
}
// #2 Template specialization
template<>
int foo<int>()
{
return 42;
}
// #3 Template specialization with template as parameter? Not working.
template<>
template<typename T>
Foo<T> foo<Foo<T>>()
{
return Foo<T>();
}
【问题讨论】:
-
Clang 对额外的
typename<>给出了很好的清晰警告。无论如何,您不能部分专门化功能模板。但是您可以部分专门化类模板。 -
我同意,这不适用于函数。总结一下
-
"模板类"正确的术语是类模板。它是生成类的模板。
-
感谢您的信息,从现在开始我将其称为“类模板”。
标签: c++ templates template-specialization