3

What is the storage duration of a temporary object: automatic, thread, static, or dynamic?

I know that the lifetime of a temporary object ends at or before the full expression where it was created, unless it is bound to a reference, in which case its lifetime is extended to that of the reference and that gives us a lower bound for the end of the storage duration of the temporary object.

Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
  • 1
    Related answer: https://stackoverflow.com/a/47945710/1625187 I don't know if the wording has been fixed since then. – Evg Sep 13 '21 at 08:54
  • @Evg Thanks. No I don’t think so. I have just filed [an issue](https://github.com/cplusplus/draft/issues/4900) on GitHub. – Géry Ogam Sep 13 '21 at 09:43

2 Answers2

5

The standard is a bit vague. It says that temporary objects can have automatic, thread or static storage duration, but within definition of those storage durations, it only specifies when variables have such duration.

The standard doesn't exactly say what the storage duration of temporary is by name in each case. Rather, it describes separately when the temporary is destroyed.

The vagueness shouldn't matter much, unless I'm mistaken. Knowing storage duration name of an object is useful for knowing its lifetime when it isn't specified otherwise, but the lifetime for temporaries is specified otherwise so the storage duration name doesn't provide additional information as far as I can tell.

Although the standard doesn't say so, it would be reasonable to assume that when lifetime of a temporary is extended by a reference, then the temporary has the same storage duration as the reference has. Otherwise, the lifetime is similar to a variable with automatic storage duration within a hypothetical block surrounding the full-expression.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3

From your own link:

Static storage duration

  • (1.1) do not have thread storage duration and

  • (1.2) belong to a namespace scope ([basic.scope.namespace]) or are first declared with the static or extern keywords ([dcl.stc])

Temporaries aren't declared at all, so they can't be declared with the static or extern keywords. They generally won't belong to namespace scope.

Thread storage duration

Only if they're explicitly declared thread_local, so that doesn't apply.

Automatic storage duration

Variables that belong to a block or parameter scope and are not explicitly declared static, thread_­local, or extern have automatic storage duration.

This is going to apply to most temporaries, since they're not explicitly declared anything, and are created by an expression, most of which live somewhere inside a block scope.

The exception is a static bound to a reference which has non-automatic storage duration: it must inherit the reference's storage duration.

The documented example is a temporary bound to a reference which has namespace scope, and must inherit static duration.

const S& cr = S(16)+S(23);

Note in that example that the temporaries used as parameters to operator+ don't get static duration just because their expression is at namespace scope: they're still parameter scope and therefore automatic.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Temporary objects aren't variables, are they? – eerorika Sep 13 '21 at 08:57
  • Nope - ah, I see the duration references are in terms of variables even though it's described as a property of objects. Will figure out how to improve. – Useless Sep 13 '21 at 08:59