Why are redundant scope qualifications supported by the compiler, and is it legal?

元气小坏坏 提交于 2019-11-26 04:52:44

问题


I tested on two compilers, and was surprised to see both support the following definition without complaint:

class A {
  A();
};

A::A::A() {}
   ^^^

Note that this also succeeds for methods, although it is flagged when the declaration is over-qualified.

Questions:

  • Is this a valid C++ program?
  • If so, what purpose does it serve - or is it merely a byproduct?

Updated Detail:

In case the original question was not clear or too short: I\'m curious why redundant qualifications are permitted on the definition (emphasis also added above).


Clang an Apple\'s GCC 4.2 + LLVM were the compilers


回答1:


Yes, it's allowed (§9/2):

The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

For information about the reasoning that lead to class name inject, you might want to read N0444.



来源:https://stackoverflow.com/questions/12135498/why-are-redundant-scope-qualifications-supported-by-the-compiler-and-is-it-lega

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