Why compilation fails when a template parameter name matches an inner class name?

一笑奈何 提交于 2019-12-01 03:12:00

问题


Following compiles perfectly fine:

struct MyClass {
  template<typename SameName>
  void foo (SameName* p);
};
struct SameName {};

template<class SameName>
void MyClass::foo (SameName* p) {}

However, if we enclose MyClass and SameName inside some class Outer then the template function defined outside, fails to compile.

struct Outer {
  /* paste here `MyClass` & `SameName` from above */
};

template<class SameName>
void Outer::MyClass::foo (SameName* p) {}  // <--- error here
//   ^^^^^

The g++ (03-14) error is weird:

error: prototype for ‘void Outer::MyClass::foo(Outer::SameName*)’ does not match any in class ‘Outer::MyClass’
 void Outer::MyClass::foo (SameName* p) {}
      ^~~~~
templateClassMethod.cpp:6:10: error: candidate is: template<class SameName> void Outer::MyClass::foo(SameName*)
     void foo (SameName* p);

The clang (03-14) error is less intuitive:

error: out-of-line definition of 'foo' does not match any declaration in 'Outer::MyClass'
void Outer::MyClass::foo (SameName* p) {}

Question:

  • Is this a language/compiler bug or an expected behaviour?
  • If expected, then why is the choice for template type's name being restricted for inner classes?

[Note: Actually I had many template parameters, and incidentally one of them matched the inner class name, by chance. It took me 1 hour to figure out. The error generated in that complex scenario was totally misleading.]

来源:https://stackoverflow.com/questions/41280133/why-compilation-fails-when-a-template-parameter-name-matches-an-inner-class-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!