Can a template be instantiated if there is nested type (not accessed) with a method that cannot be compiled?

I accidentally found that g++ (5.2.0) compiles the following

template<typename T>
struct A {
    int x;
    struct B {
        void foo() {
            x = 1;

even istantiating A and A::B, provided the member B::foo is not used. You reasonably get instead a compile error for x being a non-static member of A even by just compiling the no-op statement &A<int>::B::foo;.

clang (3.6.2) however refuses the template even if A is not instantiated at all because it says that the non-static member name x cannot be used inside B while just reading the template definition.

Is this a bug in g++ or clang is being too strict about template members that are not instantiated?

Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.