相关疑难解决方法(0)

未使用的模板方法出错

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);
}
Run Code Online (Sandbox Code Playgroud)

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(); }
                   ~~~^~~
Run Code Online (Sandbox Code Playgroud)

当然代码是错误的,但哪个编译器符合标准?

同样奇怪的是,如果使用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 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates clang non-static

4
推荐指数
1
解决办法
731
查看次数

标签 统计

c++ ×1

clang ×1

gcc ×1

non-static ×1

templates ×1