4

I'm puzzled by a compiler error regarding the visibility of an inherited member variable. I reduced it to this minimal, complete, and verifiable example:

#include <iostream>

template <typename T> class Base {
    protected:
        T m_value = T{};
};

template <typename T> class Derived : public Base<T> {
    public:
        void Foo() { std::cout << m_value; };
};

int main() {
    Derived<int> d;
    d.Foo();
}

MSVC reports:

Source.cpp(10,35): error C2065: 'm_value': undeclared identifier
Source.cpp(10,35): error C3861: 'm_value': identifier not found

I'm surprised: Derived<T> should inherit m_value from Base<T> and it should be accessible because it's protected.

I tried reducing the problem further: Replacing Base<T> with a non-templated class and declaring its m_value as int works. Keeping Base<T> as a template class and making Derived a non-templated class (that derives from Base<int>), also works. The problem is only when both are templates.

I found other StackOverflow questions with similar titles, but none of them were exactly this issue. The problems in those usually involved shadowing or syntax errors.

I tried Compiler Explorer (godbolt.org) to see whether Clang and gcc agreed with MSVC. Clang 10 also reports the error, as does gcc 10.2. Oddly, the MSVC 19.24 on Compiler Explorer happily compiles the code without errors. My local MSVC is more recent: 19.26. So the consensus of the current versions of the major C++ compilers is that the code is wrong.

Can someone explain why a protected member variable of a base class is not visible to the derived class when both the base and derived classes are template instantiations?

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • didnt find a duplciate, but I think it is this: http://www.cs.technion.ac.il/users/yechiel/c++-faq/nondependent-name-lookup-members.html – 463035818_is_not_an_ai Aug 02 '20 at 18:40
  • 2
    [Does this answer your question?](https://stackoverflow.com/questions/44416954/is-this-mandatory-to-access-baset-identifiers-from-derived-classes?rq=1) – chris Aug 02 '20 at 18:41
  • @chris: Thanks! I hadn't found that question in my search, perhaps because the title describes the solution rather than the problem. The second answer there does a pretty good job of explaining why. – Adrian McCarthy Aug 03 '20 at 01:04

0 Answers0