VS2019 c++ C3861 深度继承模板错误

kko*_*ost 2 c++ c++17 visual-studio-2019

我在 VS2019 中使用 /permissive- 编译时遇到 C3861 错误

class BaseClass
{
protected:
    void baseClassMethod()
    {
        m_value = 0;
    }

    int m_value;
};

template<typename T1>
class DerTmpl_1 : public BaseClass
{
public:
    T1 doTheThing(T1 t)
    {
        baseClassMethod();
        m_value = 123;
        return t;
    }
};

template<typename T1, typename T2>
class DerTmpl_2 : DerTmpl_1<T1>
{
public:
    T2 doTheOtherThing(T1 t1, T2 t2)
    {
        baseClassMethod();  // C3861 here, but only with /permissive-
        doTheThing(t1);     
        m_value = 456;      // C3861 here, but only with /permissive-
        return t2;
    }
};
Run Code Online (Sandbox Code Playgroud)

关于为什么 DerTmpl_2::doTheOtherThing 不能编译的任何指导?

C3861 错误输出

1>C:\Users\kevin\source\repos\cpp17-permissiveMinusTest\cpp17-permissiveMinusTest\cpp17-permissiveMinusTest.cpp(35,3): error C3861:  'baseClassMethod': identifier not found
1>C:\Users\kevin\source\repos\cpp17-permissiveMinusTest\cpp17-permissiveMinusTest\cpp17-permissiveMinusTest.cpp(37,3): error C3861:  'm_value': identifier not found
Run Code Online (Sandbox Code Playgroud)

raf*_*x07 6

您需要使用this来访问依赖于模板参数的基类的数据成员:

    this->baseClassMethod();  // C3861 here, but only with /permissive-
    doTheThing(t1);     
    this->m_value = 456;      // C3861 here, but only with /permissive-
Run Code Online (Sandbox Code Playgroud)


son*_*yao 6

请注意,该问题与深层继承层次结构无关,仅当从类模板继承时才可能发生。不会在依赖基类中查找非依赖名称,另一方面,模板中使用的依赖名称的查找将被推迟,直到模板参数已知为止。

您需要使名称依赖于依赖基类的名称(这取决于模板参数T1),例如

this->baseClassMethod();
this->m_value = 456;
Run Code Online (Sandbox Code Playgroud)

或者

BaseClass::baseClassMethod();
BaseClass::m_value = 456;
Run Code Online (Sandbox Code Playgroud)