Cas*_*mor 69 c++ templates metaprogramming
我不明白,在我看来,调用f
是完全明确的,但它无法编译expected primary-expression before ‘int’
.如果我通过调用注释掉该行f
,它编译得很好.
template<typename T>
struct A {
template<typename S>
void f() { }
};
template<typename T>
struct B : A<T> {
void g() {
this->f<int>();
}
};
Run Code Online (Sandbox Code Playgroud)
tem*_*def 129
这是由于标准的一个非常模糊的规定,如果你有一个模板试图访问类型取决于模板参数的对象中的模板函数,你必须以template
一种奇怪的方式使用该关键字:
this->template f<int>();
Run Code Online (Sandbox Code Playgroud)
typename
除了应用于函数之外,这类似于依赖类型的奇怪性.特别是,如果省略template
关键字,则解析之间存在歧义
this->f<int>()
Run Code Online (Sandbox Code Playgroud)
(你的意图),和
((this->f) < int) > ()
Run Code Online (Sandbox Code Playgroud)
没有意义(因此你的错误).这里使用关键字template
消除歧义并强制编译器识别出它正在查看对模板化成员函数的完全有效调用,而不是大量符号.
希望这可以帮助!