繁体   English   中英

当 CRTP 派生类型是模板 class 时,公共 class 成员不可见

[英]Public class member not visible when CRTP derived type is a template class

以下代码无法编译。

我希望Derived<T>访问Basem_vec成员。 但是,由于Derived<T>是模板化的,它通过以下方式实现 CRTP : public Base<Derived<T>>并且m_vec不可见。

如果我将Derived<T>更改为仅Derivedm_vec变得可见。

为什么这是/是否有解决方法?

#include <vector>

template<class SUB_CLASS>
struct Base
{
    Base(int config){}
    std::vector<std::string> m_vec;  
};

template<class T>
struct Derived : public Base<Derived<T>>
{
    Derived(int config) : Base<Derived<T>>(config){}
    
    void start()
    {
        m_vec.clear();   // This line doesn't compile. m_vec is not accessible
    }
};

int main()
{
    int config = 0;
    Derived<int> d(config);
    d.start();
    return 0;
}

使用访问成员

this->m_vec.clear();

那应该编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM