c++ - Can a template be instantiated if there is nested type (not accessed) with a method that cannot be compiled? -
i accidentally found g++ (5.2.0) compiles following
template<typename t> struct { int x; struct b { void foo() { x = 1; } }; };
even istantiating a
, a::b
, provided member b::foo
not used. reasonably instead compile error x
being non-static member of a
compiling no-op statement &a<int>::b::foo;
.
clang (3.6.2) refuses template if a
not instantiated @ because says non-static member name x
cannot used inside b
while reading template definition.
is bug in g++ or clang being strict template members not instantiated?
the x
in code non-dependent name, , standard renders template cannot validly instantiated whatever provide template parameter ill-formed, no diagnostic required (actually applies "temploids", example in standard shows. is, rules apply equally members of class templates).
your code invalid normal class accesses name of surrounding class. notice direction important here. following not ill-formed (because can specialize a<t>::b
such x
a<int>::b
static member, example.
template<typename t> struct { struct b { int x; }; void f() { b::x = 1; } };
however, enclosing class template's type called the current instantiation, means meaning stays same , cannot change depending on template parameters. therefore template definition can rendered ill-formed uses such yours.
Comments
Post a Comment