3

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

nullptr
  • 93
  • 11
  • Does this answer your question? [Why do I have to access template base class members through the this pointer?](https://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer) – Davis Herring Jan 14 '22 at 07:46

1 Answers1

4

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://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-170)

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Thank you for help. Meanwhile figured out that also __super::m_vec is doing the job. Your detailed answer is very helpful – nullptr Jan 14 '22 at 04:21
  • @nullptr Well, that is completely non-standard. If you really want the old MSVC behavior back, you can add the `/permissive` flag (without `-` at the end). See the article I linked. But then your code is not standard C++ and will work only on MSVC. – user17732522 Jan 14 '22 at 04:25
  • No thanks. This pushes me more into direction of standard coding and i appreciate your help. – nullptr Jan 14 '22 at 04:31