【发布时间】:2015-09-11 15:46:00
【问题描述】:
在声明模板特化时,带 (1) 和不带尖括号 (2) 的语法有什么区别。
如果未提供方法的实现(定义)(如本例中),为什么版本 1 会失败,并出现错误:undefined reference to int f<int>(int) 而版本 2 按预期工作?
template <typename T> T f(T val) {
return val;
}
template<> int f<int>(int val); // 1
template int f<int>(int val); // 2
int main() {
cout << f(555);
}
我见过this answer,但它没有明确描述这些不同语法之间的区别。
【问题讨论】:
标签: c++ templates syntax template-specialization