Exposing private base class name with `using` in C++

Viewed 229

In a situation when some struct D inherits another struct A twice: once privately via B and the second time publicly via C, is it allowed to write using B::A in D?

struct A {};
class B : A {};
struct C : A {};
struct D : B, C { 
    using B::A;  //ok in GCC, error in Clang
};

The program is accepted by GCC, but Clang prints the error:

error: 'A' is a private member of 'A'

Demo: https://gcc.godbolt.org/z/5jeqrzorE

using B::A must just expose injected class name A from D. On the one hand, A is already available to use in D (so GCC accepts it), but on the other hand A is private within B (so Clang rejects it). Which one compiler is right here?

1 Answers

It would somehow cause ambiguity in this case. The class lookup needs to be ignored when checking A. GCC should be correct since B::A and C::A refer to the same A from D. Even clang could not detect the error if using C::A is added

struct A {};
struct B :  A {};
struct C : A {};
struct D : C, B { 
    using B::A;  //ok in Clang, error in GCC
    using C::A;
};

https://timsong-cpp.github.io/cppwp/namespace.udecl#1

Related