Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...