小编Aar*_*ker的帖子

使用从模板化基类继承的成员变量(C++)

我试图在派生类中使用模板化基类的成员变量,如下例所示:

template <class dtype>
struct A {
    int x;
};

template <class dtype>
struct B : public A<dtype> {
    void test() {
        int id1 = this->x;      // always works
        int id2 = A<dtype>::x;  // always works
        int id3 = B::x;         // always works
        int id4 = x;            // fails in gcc & clang, works in icc and xlc
    }
};
Run Code Online (Sandbox Code Playgroud)

gcc和clang都非常挑剔使用这个变量,并且要求显式范围或明确使用"this".使用其他一些编译器(xlc和icc),事情就像我期望的那样工作.这是xlc和icc的情况,允许代码不是标准的,还是gcc和clang中的错误?

c++ compiler-construction inheritance templates

6
推荐指数
1
解决办法
1889
查看次数