Nested class using declaration and access modifiers

when compiling some code that I encountered a compiler error, which seemed strange to me, and related to inheritance, nested classes, using declarations and access modifiers.

Basically, the intention is that the derived type can expose a nested protected class of the base type.

The following short example to demonstrate the problem:

class Base { protected: struct Nested { enum value_enum { val = 0, val2, val3 }; }; }; class Derived : public Base { public: using Base::Nested; }; int main(int argc, char** argv) { //Base::Nested aa; // error, as (I) expected //Base::Nested::value_enum ab; // error, as (I) expected Derived::Nested ba; // works, as (I) expected Derived::Nested::value_enum bb; // MSVC error, as (I) did not expect return 0; } 

Watch live .

MSVC11 (v11.00.61030) throttles this code with the following error:

error C2248: "Base :: Nested": cannot access the protected structure declared in the "Base" class

Both GCC and Clang compile this correctly, so without the ability to quote the relevant parts from the standard, I would say that this is an MSVC error.

Is there any way around this using MSVC?

+6
source share
1 answer

The following workaround works for MSVC:

 class Derived : public Base { public: using Base::Nested; typedef Base::Nested::value_enum value_enum; // add this }; int main(int argc, char** argv) { //Base::Nested aa; // error, as (I) expected //Base::Nested::value_enum ab; // error, as (I) expected Derived::Nested ba; // works, as (I) expected Derived::value_enum bb = Derived::value_enum::val; // now works in MSVC return 0; } 
+1
source

All Articles