c++ - MSVC direct constructor call extension -
In this, it has been indicated that in MSVC this-> foo: foo (42); To call the Constructor directly
For Creator Delegation :
#include & lt; Iostream & gt; Struct foo {int m; Foo (int p): m (p) {std :: cout & lt; & Lt; "Foo (" <" } Foo (): m (0) {this-> foo :: foo (42); Std :: cout & lt; & Lt; "Foo ()," & lt; & Lt; M & LT; & Lt; "\ N"; }}; Int main () {foo f; Std :: cin.ignore (); }
I was surprised that it is also compiled in MSVC; Claig ++, G ++ and I believe it's illegal, e.g. [Class.ctor] / 2 "Since constructors do not have a name, they never get names in search"
However, MSVC also has a warning with / wall
And MSWC 12 Update 1 (2013) and MSVC 10 SP1 (2010), without language extension / za
.
The output is:
foo (42) foo (), 42
in both versions
Question:
- What's the name of this extension?
- Is this considered an extension?
- Is this the official description of this feature? (I have tagged this question with the [delegating-constructors] tag because it reminds me of this feature too) / Sub>
Meta-information: I'm almost sure that this question is a duplicate, because this feature is somewhat known. For example, look at the "Similar Question" Please do not hesitate to turn it off as a duplicate if you can find an answer describing this feature.
This constructor is not representative Try the following code:
#include & lt; Iostream & gt; Category C {Public: C () {std :: cout & lt; & Lt; "C" & lt; & Lt; Std :: endl; } ~ C () {std :: cout & lt; & Lt; "~ C" & lt; & Lt; Std :: endl; }}; Struct foo {int m; Cc; Foo (int p): m (p) {std :: cout & lt; & Lt; "Foo (" <" } Foo (): m (0) {this-> foo :: foo (42); Std :: cout & lt; & Lt; "Foo ()," & lt; & Lt; M & LT; & Lt; "\ N"; }}; Int main () {foo f; }
Output field is initiated twice according to "C" but only once destroyed. As noted, zneak is similar to new (this) foo (42)
.
Comments
Post a Comment