【发布时间】:2020-06-28 16:25:23
【问题描述】:
如果编译如下所示,以下程序将输出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;
}
【问题讨论】:
-
我的问题显然也在以下综合问题下得到了回答:stackoverflow.com/questions/610245/… ...但是我发现这个问题过于宽泛。如果有什么东西应该像我的一样被分解成碎片,那更容易找到。
标签: c++ c++11 function-templates