c++ - 隐藏成员模板的模板参数

标签 c++ templates standards name-lookup

来自 temp.local :

In the definition of a member of a class template that appears outside of the class template definition, the name of a member of the class template hides the name of a template-parameter of any enclosing class templates (but not a template-parameter of the member if the member is a class or function template). [ Example:

template<class T> struct A {
  struct B { /* ... */ };
  typedef void C;
  void f();
  template<class U> void g(U);
};

template<class B> void A<B>::f() {
  B b;              // A's B, not the template parameter
}

template<class B> template<class C> void A<B>::g(C) {
  B b;              // A's B, not the template parameter
  C c;              // the template parameter C, not A's C
}

— end example ]

问题是,我尝试过的每个编译器(g++、vc、icc、clang)都在 A<B>::g(C) 中处理 C。作为 A 的成员名称,并且不编译该示例。

这是一个常见的错误吗?

最佳答案

虽然您提供的链接似乎是草案并明确声明它不是任何标准的一部分 (http://eel.is/c++draft/),但草案中的这一特定条款似乎与 ISO C++ 14.6.1 第 7 段相同。

所以它确实看起来确实是一个常见的编译器错误,或者是一个与其他子句冲突并丢失的子句。我验证了该示例无法在 MacOS Clang v802.0.42 上编译)。既然你说所有主要的编译器都会在这里发出错误,我怀疑这个子句由于与其他一些子句冲突而实现起来不合理。

编辑:我还在标准社区中发现了一个讨论 here与此主题相关。讨论的深度here向我暗示这条规则是有争议的,甚至可能会被改变。

关于c++ - 隐藏成员模板的模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41313565/

相关文章:

c++ - 在成员变量上使用std::enable_if或类似方法

C++17:integer_sequence 用法与编译错误

无法让标准差在 C 中工作

c++ - C++ 标准是否规定这两种访问数组的方法是相同的还是不同的?

javascript - 如何将 DRY 原则应用于评论?

c++ - 当通过 FFI 从 Rust 调用时,您如何使用返回 shared_ptr<T> 的 C++ 函数?

c++ - 为结构(或类)行为数组赋值

c++ - 链接器错误 'unresolved external symbol' : working with templates

javascript - AngularJS 指令数据被覆盖

C++ header 保护对象和用法?