struct B
{
int a;
void foo() {a = 5;}
};
template <typename T>
struct A
{
A(int i) { B::foo(); }
A(double d) {}
};
int main()
{
A<int> a(5.0);
}
gcc 4.7.2 编译它没有错误。clang 3.4svn 抱怨:
$ clang -Wall -Wextra test.cpp
test.cpp:10:16: error: call to non-static member function without an object argument
A(int i) { B::foo(); }
~~~^~~
代码当然是错的,但是哪个编译器符合标准呢?
如果您使用 5 而不是 5.0,clang 不会像 gcc 那样打印任何“实例化”注释,这也很奇怪:
$ gcc test.cpp
test.cpp: In instantiation of ‘A<T>::A(int) [with T = int]’:
test.cpp:15:12: required from here
test.cpp:9:13: error: cannot call member function ‘void B::foo()’ without object