为什么下面的代码无效?
template <typename S, typename T>
struct B{
void f(T t, S s) {t.f<S>(s); }
};
gcc 4.3.4 提示说它“在'>'标记之前需要主表达式”,即“S”不是一个有效的主表达式。
最佳答案
您需要指定 f
是一个模板:
void f(T t, S s) {
t.template f<S>(s);
}
自 f
以来,C++ 不知道这一点(此时)的类型取决于模板参数的类型 T
.此外,以下语法会产生歧义:does <
是指模板列表的开头还是小于运算符?为了帮助 C++ 弄清楚你需要指定 f
是模板,否则 C++ 无法解析后面的部分,因为解析本身 取决于 T
的类型.
关于c++ - 使用模板参数作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3015964/