c++ - error in unused template method -


struct b {     int a;     void foo() {a = 5;} };  template <typename t> struct {     a(int i) { b::foo(); }     a(double d) {} };  int main() {     a<int> a(5.0); } 

gcc 4.7.2 compiles without errors. clang 3.4svn complains:

$ clang -wall -wextra test.cpp  test.cpp:10:16: error: call non-static member function without object argument         a(int i) { b::foo(); }                    ~~~^~~ 

of course code wrong, compiler conformant standard?

it's strange clang doesn't print 'in instantiation' note gcc if use 5 instead of 5.0:

$ gcc test.cpp  test.cpp: in instantiation of ‘a<t>::a(int) [with t = int]’: test.cpp:15:12:   required here test.cpp:9:13: error: cannot call member function ‘void b::foo()’ without object 

your program incorrect, , both compilers right standard not require diagnostic conforming compiler (letting gcc ignore it). template there can no valid instantiation (specialization in standard jargon) incorrect if template never instantiated.

in case, name b::foo() inside a<t>::a(int) non-dependent name, needs resolved during first phase lookup, , can refer b class defined above. because not static member function, non-static one, code incorrect, regardless of type t used instantiate a<t> template , program ill-formed.

the relevant quote 14.6 [temp.res]/8:

knowing names type names allows syntax of every template definition checked. no diagnostic shall issued template definition valid specialization can generated. if no valid specialization can generated template definition, , template not instantiated, template definition ill-formed, no diagnostic required.


Comments