سؤال

Help me understand this... see bold. From the Standard 3.6.3 Termination (2)

2 If a function contains a block-scope object of static or thread storage duration that has been destroyed and the function is called during the destruction of an object with static or thread storage duration, the program has undefined behavior if the flow of control passes through the definition of the previously destroyed blockscope object. Likewise, the behavior is undefined if the block-scope object is used indirectly (i.e., through a pointer) after its destruction.

Manager& GetManager()
{
    static Manager localMan;
    return localMan;
}

Then somewhere else...

{
   static User localUser;
   localUser.DoSomething(); //localUser calls GetManager and uses the reference returned.
}

Then in the User destructor...

User::~User()
{
   GetManager().DoSomethingOneLastTime();
}

//Now lets say Main exits and static destruction begins.
//Somehow localMan is destructed before User.
//Then user calls the GetManager() function in it's destructor.
//What case is this defined, and what case makes this undefined?

Is the standard saying that if the local static localMan object that was statically created with the odr-use rule was destructed, and then the function was called again (creating a new static or not) this is undefined? It looks like it leaves room for there is defined behavior, but if it passes through the definition of the destructed object it is not.

Anyone have clear insight on this?

هل كانت مفيدة؟

المحلول

I believe the key phrase here is

creating a new static or not

If this happens during destruction of static objects, and would cause a recreation of some of the already destroyed static objects, when would they then be destroyed? What if it causes a loop? How is the system supposed to be tracking all of this? Are the possible benefits worth the trouble?

Probably not, as the language standard explicitly decides not to define this.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top