Direct extension to the MSVC constructor call

In this answer, tloveless indicated that this->foo::foo(42); can be used in MSVC this->foo::foo(42); To delegate a constructor to directly invoke the constructor:

 #include <iostream> struct foo { int m; foo(int p) : m(p) { std::cout << "foo("<<p<<")\n"; } foo() : m(0) { this->foo::foo(42); std::cout << "foo(), " << m << "\n"; } }; int main() { foo f; std::cin.ignore(); } 

I was surprised that this even compiles in MSVC; clang ++, g ++ and I agree that it is illegal, for example. [class.ctor] / 2 "Since the constructors have no names, they were never found while searching for the name"

However, MSVC does not even issue a warning with /Wall and without /Za language extensions in MSVC12 Update 1 (2013) and MSVC10 SP1 (2010).

Output:

 foo (42)
 foo (), 42

in both versions. Thus, a temporary creation is not created, but the constructor is called.

Questions:

  • What is this extension called?
  • Isn't that considered an extension? ( /Za and the list of extensions do not seem to think so)
  • Is there any documentation for / officially describing this feature?

(I marked this question with the tag [delegation-constructors], since it strongly reminds me of this function)


meta-info: I'm pretty sure this question is a duplicate, as this function is somewhat known. For example, see this answer to a "similar question." Please feel free to close this as a dup if you find an answer describing this feature.

+8
c ++ visual-c ++ language-extension delegating-constructor
source share
1 answer

This is not constructor delegation. Try entering the following code:

 #include <iostream> class C{ public: C() { std::cout << "C" << std::endl; } ~C() { std::cout << "~C" << std::endl; } }; struct foo { int m; C c; foo(int p) : m(p) { std::cout << "foo("<<p<<")\n"; } foo() : m(0) { this->foo::foo(42); std::cout << "foo(), " << m << "\n"; } }; int main() { foo f; } 

According to the output field, "c" is initialized twice, but destroyed only once. As zneak noted, it is similar to new (this) foo(42) .

+1
source share

All Articles