Templates: template function not playing well with class#39;s template member function(模板:模板函数不能很好地与类的模板成员函数配合使用)
问题描述
这是我实际拥有的一些代码的最小测试用例.当它尝试评估 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.
这篇关于模板:模板函数不能很好地与类的模板成员函数配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板:模板函数不能很好地与类的模板成员函数配
基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
