What happens when a constructor function calls itself in VS2013?

  • A+
class IA {    public:    virtual void Print() = 0; } IA* GetA();  class A : public IA {    public:    int num = 10;    A()    {      GetA()->Print();    }    void Print()    {      std::cout << num << std::endl;    } }  IA* GetA() {    static A a;    return &a; }  int main() {    GetA();    std::cout << "End" << std::endl;    getchar();    return 0;  } 
  1. Obviously, class A's constructor function calls itself.
  2. "static A a" will get stuck in a loop.
  3. On VS2013, this code can get out from the loop and print "End" on the console.
  4. On VS2017, this code gets stuck in a loop.

    **What does VS2013 do for this code?


Nothing in particular has to happen. According to the C++ standard:

[stmt.dcl] (emphasis mine)

4 Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined. [ Example:

int foo(int i) {   static int s = foo(2*i);      // recursive call - undefined   return i+1; } 

 — end example ]

The statement I emboldened is exactly what happens in your program. It's also what the standard's example shows as undefined. The language specification says an implementation can do whatever it deems appropriate. So it could cause an infinite loop, or it may not, depending on the synchronization primitives your implementation uses to prevent concurrent reentry into the block (the initialization has to be thread safe).

Even before C++11, the behavior of recursive reentry was undefined. An implementation could do anything to make sure an object is initialized only once, which in turn could produce different results.

But you can't expect anything specific to happen portably. Not to mention undefined behavior always leaves room for a small chance of nasal demons.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: