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

VS2019 c++ C3861 error in deep inheritance templates

我在 VS2019 中遇到 /permissive- 编译的 C3861 错误,当时我有一个很深的继承层次结构,派生程度最高的模板从根 derived-from class

访问受保护的符号
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;
    }
};

关于 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

您需要使用 this 来访问基 class 的数据成员,这取决于模板参数:

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

请注意,该问题与深度继承层次结构无关,它可能仅在从 class 模板继承时发生。非依赖名称将不会在依赖基础 classes 中查找,另一方面,模板中使用的 the lookup of a dependent name 被推迟到模板参数已知时才使用。

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

this->baseClassMethod();
this->m_value = 456;

或者

BaseClass::baseClassMethod();
BaseClass::m_value = 456;