【发布时间】:2020-07-16 18:10:50
【问题描述】:
有谁知道为什么template<class T1, class T2> void A<T1, T2, 4>::f0() 编译失败?我会认为这是一个专业化?
这些理解也正确吗?
- 对于
// A,模板参数的值用于构建参数列表,该列表用于通过将类定义为class A<A0, ..., An> {}来实例化模板。 - 对于
// B,当编译器需要为f0()生成代码时,它会搜索与函数关联的类的模板定义。如果找到专用版本,则使用该版本。如果没有找到专用版本,则选择非专用版本A<T1, T2, I>,其中模板参数是从表单中推导出来的。
谢谢!
// A
template<class T1, class T2, int I>
class A {
public:
void f0();
}; // primary template
// B
template<class T1, class T2, int I>
void A<T1, T2, I>::f0()
{
cout << "x" << endl;
}
template<class T1, class T2>
void A<T1, T2, 4>::f0()
{
cout << "x" << endl;
}
template<>
void A<int, int, 4>::f0()
{
cout << "z" << endl;
}
编译:
clang++ -std=c++11 -pedantic -Wall test176.cc && ./a.out
test176.cc:17:20: error: nested name specifier 'A<T1, T2, 4>::' for declaration does
not refer into a class, class template or class template partial specialization
void A<T1, T2, 4>::f0()
【问题讨论】:
-
这能回答你的问题吗? C++ partial method specialization