模板类:未继承静态成员

Template class: static members not inherited

本文关键字:继承 静态成员      更新时间:2023-10-16

以下代码

template<int c>
struct Base
{
    static const int a = c + 5;
};
template<int c>
struct Derived : Base<c>
{
    static const int b = a + 5;
};

由于a was not declared in this scope。明确指定Base<c>::a是可行的,但从逻辑上讲,这不应该是必要的,因为我们是从Base<c>派生的。这是故意的行为(为什么)还是我遗漏了什么?

template<int c>
struct Derived : Base<c>
{
    static const int b = Base<c>::a + 5;
};

这实际上取决于编译器,但gcc需要它,当涉及到模板类

时,它就显得很愚蠢