来自 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/