c++20 msvc error c3861 base member not found

Viewed 309

i have a strange problem since i switched the C++ language standard in VS2022 for my c++ project from 17 to 20.

I get compiler error C3861 identifier not found. The indentifier in this case is a member of the base class.

base class:

template <class C> class base_vector
{
public:
    base_vector()
    {
    }
    virtual ~base_vector()
    {
    }
protected:
    std::vector<C>  m_vec;
};

derived class:

template <class C> class child_vector : public base_vector<C>
{
public:
    child_vector()
    {
    }
    virtual ~child_vector()
    {
        m_vec.clear(); // c3861
    }
};

When i switch the standard to c++17, no compiler error.

m_vec should be known in child_vector but it's not. Can anyone see the problem ? Many thanks

1 Answers

m_vec is a member of a base class dependent on the template parameters.

Until instantiation of the template, the compiler cannot know that m_vec is indeed a member of the base class, but it must do name lookup for m_vec when parsing the template definition, because m_vec isn't a dependent name.

You need to use this->m_vec explicitly. this is dependent and this form will delay the name lookup until the template parameters and the base class are known.

That has also been the case before C++20. By default MSVC does however do name lookup in a non-standard-compliant way, which is why it works by default. To get the standard-compliant behavior you need to give MSVC the /permissive- flag. With the /std:c++20 flag or later, that flag is implied. (https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-170)

Related