为什么使用using声明引入的私有基类中的成员模板不可访问

Why is a member template from private base class introduced with a using declaration inaccessible?

本文关键字:成员 访问 基类 using 声明 为什么      更新时间:2023-10-16

考虑以下(人工)示例:

class A {
 public:
  template <typename T>
  class C {};
};
class B : private A {
 public:
  using A::C;
};
int main() {
  B::C<int> c;
}

它使用GCC和Clang都能成功编译,但Visual C++2010给出了以下错误:

test.cpp(13):错误C2247:"A::C"不可访问,因为"B"使用"private"从"A"继承

这是Visual C++中的一个错误,还是这段代码确实无效?

如果C不是模板,则代码将在所有编译器上编译。

[namespace.udcl]/p18:

使用声明创建的别名具有成员声明的可访问性

这里没什么好说的。名称B::C是可公开访问的,并且代码格式良好。只是另一个微软风投漏洞。