Constructor inheritance from a private template in C ++

Why is class D compiling, but class C not working?

 class A { public: A(int) {} }; template <class T> class B : private T // Note: private base class { public: using T::T; }; class C : public B<A> { public: C() : B<A>(123) {} // Error: 'class AA::A' is inaccessible }; // within this context using BA = B<A>; class D : public BA { public: D() : BA(123) {} // OK }; 

I tested GCC, Clang, and Visual C ++, and they are all the same. Changing class B : private T to public T solves the problem. But why? (Note that using T::T is public .)

+46
c ++ language-lawyer templates
Aug 23 '16 at 12:58
source share
1 answer

Class A contains the entered name of class A in its scope (that is, A::A belongs to class A , unless this is constructor related).

Class B inherits this, so the name A within region B refers to the entered class A in region A However, since A is a private base class of B , all names in the domain A are private inside B

Class C inherits this again, but it cannot access this A , since it is private within B Hence the error. Note that the error is actually related to using the name A in the B<A> construct.

The class BA does not have this problem, since the definition of B<A> not within the scope of any class, therefore the name A refers to the global name A , and not to any class-name entered. And, of course, the name BA is publicly available.

You can easily solve this by assigning the name A to C :

 class C : public B<A> { public: C() : B<::A>( 123 ) {} }; 

Note that constructor inheritance is not valid. The problem is access to the class name A (entered in A and inherited in B and C ), and not access to the constructor.

+41
Aug 23 '16 at 13:07 on
source share



All Articles