如果两者都是模板,为什么基类的成员在继承的类中不可见?

问题描述

这个最小的示例可以完美地编译:

struct Ai {
  static constexpr int x{};
};  
struct Bi : Ai {
  auto f() const {
    return x; // <------ Ai::x
  }   
};
int main() { Bi().f(); }

但是,如果类型是模板:

template <typename T>
struct A { 
  static constexpr T x{};
};
template <typename T>
struct B : A<T> {
  auto f() const {
    return x; // <------ A<T>::x
  }   
}; 
int main() { B<int>().f(); }

g ++(9.3.0)抱怨如下:

$ g++ -O3 -Wall -Wextra -pedantic -std=c++17 a.cpp
error: ‘x’ was not declared in this scope

可以return x;替换为return A<T>::x;,符号对于编译器将是可见的,但是如果A的模板参数更加复杂,我会d避免重复。

  • 为什么会发生?
  • 我可以以某种方式使xB<T>中隐式可见。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)