C++11:模板方法的模板函数调用无法编译?
C++11: template function call of a template method doesn't compile?
如果按所示编译,则以下程序输出size: 1 align: 1
。
但是,尝试从模板化函数进行相同的方法模板调用不起作用。
如果我#if 0
更改为#if 1
g++ 9.2.1 会给我错误expected primary-expression before 'char'
. clang++ 提供了一个听起来更有帮助error: use 'template' keyword to treat 'log' as a dependent template name
但我不确定它希望模板发生在哪里。
那么什么给了呢?
#include <iostream>
using namespace std;
class Foo {
public:
Foo() {};
~Foo() {};
void log( int iSizeItem, int iAlignItem ) {
cout << "size: " << iSizeItem << " align: " << iAlignItem << endl;
}
template<class T> void log() {
log( sizeof( T ), alignof( T ) );
}
};
#if 0
template<class T> void Test( T& t ) {
t.log<char>();
}
#endif
int main( int nArg, char* apszArg[] ) {
Foo foo;
foo.log<char>();
//Test( foo );
return 0;
}
您需要指定log
是函数模板,如下所示:
template<class T> void Test( T& t ) {
t.template log<char>();
}
否则编译器不知道log
是否是T
的成员,而<
实际上是operator<
。
这是一个演示。
相关文章:
- C++:编译时检查匹配的函数调用对?
- C++11:模板方法的模板函数调用无法编译?
- 内联扩展编译为函数调用 [C++]
- 为什么缺少名称的函数调用仍然可以编译
- C++:编译对无关派生类、bug或特性的函数调用
- 函数调用中出现意外编译错误 (C++)
- C函数调用C 成员函数 - 其中C代码与C编译器编译
- 编译时动态函数调用
- 重载函数调用编译时常量
- 使用函数调用初始化静态全局数据(在编译时)
- 如何告诉编译器向编译的每个函数添加一个函数调用
- GNU g++ 4.9.2 查找函数调用的编译错误
- C++中的编译时条件成员函数调用
- 连续函数调用的编译时检查
- 注释函数的内容,但保持对函数的调用不变.编译器是否弄清楚不编译函数
- 这个C++代码有什么问题?编译错误:调用“测试::测试(测试)”没有匹配函数
- 错误:对的调用没有匹配的函数调用-使用VS2013编译
- C++中函数调用次数的编译时间限制
- C++VS2015 constexpr编译错误,constexpr构造函数调用constexpr成员函数
- 在C++中,在编译时强制执行函数调用