while compiling some code that I ran into a compiler error that seemed strange to me and relates to inheritance, nested classes, using declarations, and access modifiers.
Basically, the intention is for a derived type to expose a nested protected class of a base type.
The the following short example to demonstrate the issue:
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;
}
See live.
MSVC11 (v11.00.61030) chokes on this code with the following error:
error C2248: 'Base::Nested' : cannot access protected struct declared in class 'Base'
Both GCC and Clang compile this properly, so without the ability to quote the relevant parts from the standard, I would argue this is an MSVC bug.
Is there a way around this with MSVC?
Derived
changes the access modifier forNested
; it is not protected in this case. Also,value_enum
is not protected insideNested
. – Amperehourtypedef Base::Nested::value_enum value_enum;
inDerived
class. – Damoiselletypedef Base::Nested Nested
(There are other types, etc; since is was only an SSCCE). I "like" that idea as a workaround. – Amperehour