1

I would like to know where is this form of constructor calling is documented. This syntax apparently works since Visual Studio version 6.0 (I know it does not compile using G++).

Please note that I am not looking for alternatives and I don't need to know that it's good or evil.

class Foo
{
public:
    int m_value;
    Foo() : m_value(0) {}
};

Foo o;
o.m_value = 5;
o.Foo::Foo(); // Explicit constructor call!
EXPECT_EQ(0, o.m_value); // True!

I first found this syntax reading this article:

http://www.dreamincode.net/forums/topic/160032-finite-state-machines/

This post also refers to this syntax as well:

Can I call a constructor from another constructor (do constructor chaining) in C++?

Another post discussing the matter:

Explicit constructor call in C++

Community
  • 1
  • 1
Maestro
  • 348
  • 4
  • 13

2 Answers2

4

The supposed explicit constructor call is not valid C++ syntax. The fact that MSVC accepts such code is a bug.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • How such (easy and obvious) bug remain since Visual Studio 6.0 up to (at least) Visual Studio 2012 (for what I could test)? – Maestro Oct 28 '13 at 20:42
  • 1
    Is this a rhetorical question? In any case, I personally am in no position to answer it. – Igor Tandetnik Oct 28 '13 at 20:51
  • I understand there are likely no answer to this. It looks like a bug (Microsoft does not acknowledge it clearly in the report thread), the fact that it remains in VS2012 let me believe it works as design even though it may never have been an intended feature! :)) – Maestro Oct 28 '13 at 21:21
  • 1
    Compiler bugs that allow invalid code to compile are generally of lower priority than bugs that prevent legal code from compiling, which in turn are of lower priority than bugs that allow legal code to compile but assign wrong meaning to it. It's possible Microsoft has its hands full with the latter two categories. The former category, after all, has an easy workaround - just don't write invalid code in the first place. – Igor Tandetnik Oct 28 '13 at 21:26
0

Its of no use since you are creating a transient object in between and it dies when the scope ends.What ever value contain in object o remains the same, so u got the True value

Tony Thomas
  • 967
  • 10
  • 20
  • The author of the state machine was using it this way: (I know the same could be achieved using placement-new or overloading the new operator if the intend was to reduce memory allocations... but still, the trick is interesting in that it kind of works!) `#define convert_to(target) reinterpret_cast(this)->target::target()` – Maestro Oct 28 '13 at 18:54