【发布时间】:2010-12-13 13:28:32
【问题描述】:
这是我实际拥有的一些代码的最小测试用例。它在尝试评估 a.getResult<B>() 时失败:
test.cpp: In function 'void printStuff(const A&)':
test.cpp:6: error: expected primary-expression before '>' token
test.cpp:6: error: expected primary-expression before ')' token
代码是:
#include <iostream>
template< class A, class B>
void printStuff( const A& a)
{
size_t value = a.getResult<B>();
std::cout << value << std::endl;
}
struct Firstclass {
template< class X >
size_t getResult() const {
X someInstance;
return sizeof(someInstance);
}
};
int main(int, char**) {
Firstclass foo;
printStuff<Firstclass, short int>(foo);
printStuff<Firstclass, double>(foo);
std::cout << foo.getResult< double >() << std::endl;
return 0;
}
如果我注释掉 printStuff 函数及其调用位置,则 foo.getResult< double >() 调用可以正常编译并执行预期操作。
知道发生了什么吗?我一直在使用大量模板代码,但从未遇到过这样的事情。
【问题讨论】: