Is it possible to create an instance of a template if there is a nested type (not available) using a method that cannot be compiled?

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

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

even istantition Aand A::Bif the item is B::foonot used. You reasonably get instead a compilation error for x, which is a non-static member A, even just by compiling the no-op operator &A<int>::B::foo;.

clang (3.6.2), however, discards the template, even if Ait is not created at all, because it says that the name of the non-static member xcannot be used internally Bby simply reading the template definition.

Is this a bug in g ++ or clang too strict for template members that are not created?

+4
2

x - , , Standard , , , , ( "temploids" ", . , ).

, , . , . ( A<T>::B, x A<int>::B , .

template<typename T>
struct A {
    struct B {
       int x;
    };

    void f() {
       B::x = 1;
    }
};

, , . , .

+2

[temp.res]/8 :

, . , , , .

, B. , , , -.

+1

Source: https://habr.com/ru/post/1610080/


All Articles