Template class derived from private member class breaks encapsulation

Viewed 105

I've found what appears to be a strange method to break encapsulation of a class derived within a class

take the following

class Base
{
    private:
        class Member
        {
            public:
                virtual ~Member() {}

                virtual void Function() = 0;
         };
};

if you try to derive from this private class

class DerivedMember : public Base::Member
{
    public:
        DerivedMember() {}

        virtual void Function() {/*Do something*/}
};

you get a compile error

error: 'class Base::Member' is private
error: within this context

however if you derive from it a template

template <typename T>
class TemplateDerivedMember : public Base::Member
{
    public:
        TemplateDerivedMember () {}

        virtual void Function() {/*Do something*/}
};

then the compiler will accept it, and allow this to happen.

i've tested this against GCC 8.1 and GCC 4.6, and both have the same behaviour. is this a bug, or is it something that should be allowed? does deriving it as a template class implicitly place the TemplateDerivedMember class as a member of Base? (it doesn't appear to as it accessible in the global namespace)

1 Answers
Related