下面的程序用 Clang 编译得很好:
template< typename > struct X
{
void foo() {}
auto bar() -> decltype( X::foo() )
{
return foo();
}
};
int main()
{
X<int>().bar();
}
但是 GCC 4.8.1 给出了:
main.cpp: In instantiation of 'struct X<int>':
main.cpp:13:10: required from here
main.cpp:5:34: error: cannot call member function 'void X< <template-parameter-1-1> >::foo() [with <template-parameter-1-1> = int]' without object
auto bar() -> decltype( X::foo() )
^
main.cpp: In function 'int main()':
main.cpp:13:12: error: 'struct X<int>' has no member named 'bar'
X<int>().bar();
^
当我将代码更改为 decltype( std::declval<X>().foo() )
时GCC 对其进行编译。
这是 GCC 中的一个错误(是否有针对它的现有错误报告或应该报告它?)或者我的代码有什么问题吗?
最佳答案
可能是这个错误? decltype needs explicit 'this' pointer in member function declaration of template class with trailing return type – 动态
关于c++ - 未评估上下文中的 "Cannot call member function ... without object"- GCC 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20312062/