c++ - 模板类 : static members not inherited

标签 c++ templates

<分区>

下面的代码

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 需要它,它在模板类方面表现得很愚蠢

关于c++ - 模板类 : static members not inherited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147233/

相关文章:

c++ - Make++o++ 提示具有用户定义的前置和后置增量运算符的类型

c++ - 模板 (C++) - 不确定是否正确

c++ - 在模板特化中模拟开关组件

css - 响应模式在使用 Artisteer 制作的 Joomla 模板中不起作用

c++ - 使用 std::move 将参数传递给函数,如果将该参数声明为按值传递或使用 move 操作数 && ,是否有区别?

c++ - 无法在 C++ 中重载输出流

wordpress - 在页面上显示最近的自定义帖子类型的内容

允许任意类输入的C++函数

c# - 从C#事件触发C++操作

c++ - Qt读取文本文件的一些信息