为什么派生模板类无法访问其父模板类的公共数据成员

时间:2016-02-20 18:50:54

标签: c++ templates

这个标题有点误导。这是我的问题:

在下面的简单示例中, A 的公共数据成员 的类型为 T B A 的派生类,因为没有歧义,我们可以在名称 A :: a > B :: fun 中的。但事实证明,g ++(4.7.0)会抱怨错误:'a'未在此范围内声明

如果 A B 是正常类,则不存在此类问题。

我的问题是:这是c ++编译器的标准行为吗?那是什么 这种行为的理由?

template<typename T>
struct A {

    T a;
};

template<typename T>
struct B: public A<T> {

    void fun() {

        // does not compile
        // for normal class (non-template class), this is ok
        a = 10;

        // ok
        A<T>::a = 10;
    }
};

int main(void) {

    return 0;
}

0 个答案:

没有答案
相关问题