这是我实际拥有的一些代码的最小测试用例.当它尝试评估 a.getResult() 时失败:
This is a minimal test case of some code that I actually have. It fails when it tries to evaluate 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 >() 调用编译良好,并符合预期.
If I comment out the printStuff function and where it's called, the foo.getResult< double >() call compiles fine and does what is expected.
知道发生了什么吗?一段时间以来,我一直在使用广泛的模板化代码,但从未遇到过这样的事情.
Any idea what's going on? I've been working with extensively templated code for a while and have never encountered anything like this.
当您引用属于依赖类型成员的模板时,您必须在它前面加上关键字 template.这就是对 printStuff 内的 getResult 的调用应该是什么样子
When you refer to a template that is a member of dependent type, you have to prepend it with a keyword template. This is how the call to getResult inside printStuff should look
size_t value = a.template getResult<B>();
这类似于在引用依赖类型中的嵌套类型名时使用关键字 typename.出于某种原因,关于typename 嵌套类型的部分是众所周知的,但是对于template 嵌套模板的类似要求却相对未知.
This is similar to using the keyword typename when referring to nested typenames in a dependent type. For some reason, the bit about typename with nested types is rather well-known, but the similar requirement for template with nested templates is relatively unknown.
请注意,一般的语法结构有点不同.typename 总是放在类型的全名前面,而 template 插入在中间.
Note that the general syntax structure is a bit different though. The typename is always put in front of the full name of the type, while template is inserted in the middle.
同样,这仅在您访问依赖类型的模板成员时才需要,在上面的示例中为 printStuff 中的 A代码>.当您在 main 中调用 foo.getResult<> 时,foo 的类型是不相关的,因此不需要包含 模板关键字.
Again, this is only necessary when you are accessing a template member of a dependent type, which in the above example would be A in printStuff. When you call foo.getResult<> in main the type of foo is not dependent, so there's no need to include the template keyword.
这篇关于模板:模板函数不能很好地与类的模板成员函数配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么两个函数的地址相同?Why do two functions have the same address?(为什么两个函数的地址相同?)
为什么 std::function 的初始化程序必须是可复制构Why the initializer of std::function has to be CopyConstructible?(为什么 std::function 的初始化程序必须是可复制构造的?)
混合模板与多态性mixing templates with polymorphism(混合模板与多态性)
我什么时候应该使用关键字“typename"?使用模When should I use the keyword quot;typenamequot; when using templates(我什么时候应该使用关键字“typename?使用模板时)
依赖名称解析命名空间 std/标准库Dependent name resolution amp; namespace std / Standard Library(依赖名称解析命名空间 std/标准库)
gcc 可以编译可变参数模板,而 clang 不能gcc can compile a variadic template while clang cannot(gcc 可以编译可变参数模板,而 clang 不能)