Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a temporary object with static storage duration

Inspired from this answer, from [expr.const]

A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints:

  • if the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression,

  • if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object ([expr.add]), the address of a function, or a null pointer value, and

  • if the value is an object of class or array type, each subobject satisfies these constraints for the value.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.

What exactly is a temporary object with static storage duration? Am I missing something or is it paradoxical for an object to both be temporary and have static storage duration?

The definition from [basic.stc.static]

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program

Applies to variables only.

like image 396
Passer By Avatar asked Dec 22 '17 17:12

Passer By


People also ask

What is static storage duration?

The static storage duration means that the variable will last for the entire execution time of the program. This means that a static variable will stay valid in the memory till the program is running.

Which objects have automatic storage duration?

All objects in a program have one of the following storage durations: automatic storage duration. The storage for the object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared static , extern or thread_local .

What is object lifetime in c++?

The lifetime of an object begins when its initialization is complete, and ends when its storage is released. Dynamic storage duration starts when the storage created by (new Type) is initialized, and ends when the object goes out of scope or is deleted by “delete pointer”.


1 Answers

[basic.stc]/1 tells us:

The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object.

So every object has a storage duration. Further, paragraph 2 says:

Static, thread, and automatic storage durations are associated with objects introduced by declarations (6.1) and implicitly created by the implementation (15.2).

Emphasis added. Note that section 15.2 is [class.temporary]: the rules for temporary objects.

Therefore, we can conclude that temporary objects have storage durations. And we can conclude that temporaries must have one of those storage durations. Indeed, there are numerous references in the standard to "variables or temporary objects" and their storage durations.

However, despite this clearly saying that temporary objects have one of those storage durations... the standard never actually says what storage duration they have. [class.temporary] does not have a statement saying that temporaries bound to references have the storage duration of their references. And [basic.stc]'s explanation of static, automatic, and thread-local durations always speaks of variables.

So I would say that this is a defect in the wording. It seems clear that the standard expects temporaries to have an appropriate storage duration; there are multiple places where the standard talks about the storage duration of variable or temporary objects. But it never says what storage duration they actually have.

like image 69
Nicol Bolas Avatar answered Oct 11 '22 12:10

Nicol Bolas