15

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.

Community
  • 1
  • 1
Passer By
  • 19,325
  • 6
  • 49
  • 96

1 Answers1

11

[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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Even if the temporary storage duration is not mentionned, it can be infered from [class.temporary]/6 [The temporary object to which the reference is bound \[...\] persists for the lifetime of the reference...](http://eel.is/c++draft/class.temporary#6). Nota: Considering that a temporary with static storage duration is necessarily bound to a reference with static storage duration. – Oliv Dec 22 '17 at 19:07
  • @Oliv: Lifetime and storage duration aren't the same thing. Lifetime is about when the object is a legal C++ object and can have legal, C++ object things done to it. Storage duration is about when the memory backing that object stops existing. – Nicol Bolas Dec 22 '17 at 19:15
  • @NicolBolas Indeed but since an object as an associated storage, the deduction of storage duration seems trivial no? – Oliv Dec 22 '17 at 19:17
  • @Oliv: Objects occupy storage, but that doesn't mean storage requires objects. – Nicol Bolas Dec 22 '17 at 19:18
  • [An object occupies a region of storage in its period of construction (\[class.cdtor\]), throughout its lifetime (\[basic.life\]), and in its period of destruction](http://eel.is/c++draft/intro.object#1) – Oliv Dec 22 '17 at 19:19
  • @Oliv: But it does not say "a region of storage has objects in it". Storage exists *independently* of object lifetime. Hence why you can destroy an object and create a new one in the same storage. – Nicol Bolas Dec 22 '17 at 19:19
  • I do not get what's the object or what is the implication of your last comment. Could you develop? – Oliv Dec 22 '17 at 19:29
  • The only weakness of the argumentation above is that possibly: the duration of "static storage duration" must include the duration of the storage of the temporary which must include the duration of a static reference. This subtility has no incidence, this temporary storage duration belong to the class of "static storage duration". – Oliv Dec 22 '17 at 19:41
  • @Oliv: `int i = 0; float *f = new(&i) float(1.0f);` The object `i` is destroyed by the `new` statement; it's storage *remains*. – Nicol Bolas Dec 22 '17 at 19:41
  • @Oliv: "*The only weakness of the argumentation above*" You mean, besides the fact that there's not one statement in the standard linking a temporary's storage duration to that of a reference to which it is bound? "Storage duration" and "lifetime" are distinct concepts in C++; they are related only in that "lifetime" certainly ends when "storage duration" does. But the fact that "lifetime" has ended *does not* guarantee that "storage duration" has ended. – Nicol Bolas Dec 22 '17 at 19:42
  • Every object occupies a region of storage. Each storage has a duration class. The object must live as long as live a static reference. Which is the storage duration class of the object my dear Watson? Static, dynamic, automatic or thread_local – Oliv Dec 22 '17 at 20:23
  • @Oliv: This question is tagged "language-lawyer", so the question is not "how does this likely work". It's "how does the standard *say* that it works". So if the standard does not have a line *explicitly* detailing the storage duration of temporaries, then the standard does not say. – Nicol Bolas Dec 23 '17 at 00:18
  • Interestingly, all the paragraph of your answer begin by conjunctive adverb. Check it. Read again your answer. Is there any deduction it? Now read my comments is there any deduction in it? Now what is the difference in nature between these comments and your answer? So what can you deduce about yourself? – Oliv Dec 23 '17 at 09:32
  • I have just filed [an issue](https://github.com/cplusplus/draft/issues/4900) on GitHub. – Géry Ogam Sep 13 '21 at 09:42