同
template <typename T>
class Foo {
public:
template <int x>
void bar () {}
};
Run Code Online (Sandbox Code Playgroud)
以下编译:
void fooBar ()
{
Foo<int> f;
f.bar<1>();
}
Run Code Online (Sandbox Code Playgroud)
但是以下内容没有(带有"错误:在'之前预期的primary-expression')'令牌"在gcc 5.4.0中使用-std = c ++ 14).
template <typename T>
void fooBar ()
{
Foo<T> f;
f.bar<1>();
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试显式调用第二个版本,例如
fooBar<int>();
Run Code Online (Sandbox Code Playgroud)
然后gcc另外抱怨
"invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'".
Run Code Online (Sandbox Code Playgroud)
有没有理由说第二个版本无效?为什么gcc将'<'视为运算符而不是模板参数列表的开头?
Bo *_*son 15
使用模板化函数,编译器不确切知道Foo<T>将会是什么(可能有特化Foo),因此它必须假设它f.bar是一个成员变量并将代码解析为
f.bar < 1
Run Code Online (Sandbox Code Playgroud)
然后它无法继续.
您可以通过告诉它bar是模板来帮助编译器
f.template bar<1>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |