Where Are Temporary Object Stored

Where is the temporary object stored?

It's stored on the stack, not the heap. Note however that std::string probably makes use of the heap.

What is a temporary object with static storage duration

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

Where are temporary objects allocated in C++?

If created at all (consider optimizations), they're in automatic storage. I.e. the stack.

rvalue references: what exactly are temporary objects, what is their scope, and where are they stored?

Firstly it is important not to conflate the terms "rvalue" and "temporary object". They have very different meanings.


Temporary objects do not have a storage duration. Instead, they have lifetime rules that are specific to temporary objects. These can be found in section [class.temporary] of the C++ Standard; there is a summary on cppreference, which also includes a list of which expressions create temporary objects.

In practice I'd expect that a compiler would either optimize the object out, or store it in the same location as automatic objects are stored.

Note that "temporary object" only refers to objects of class type. The equivalent for built-in types are called values. (Not "temporary values"). In fact the term "values" includes both values of built-in type, and temporary objects.

A "value" is a completely separate idea to prvalue, xvalue, rvalue. The similarity in spelling is unfortunate.

Values don't have scope. Scope is a property of a name. In many cases the scope of a name coincides with the lifetime of the object or value it names, but not always.


The terms rvalue, lvalue etc. are value categories of an expression. These describe expressions, not values or objects.

Every expression has a value category. Also, every expression has a value, except expressions of void type. These are two different things. (The value of an expression has a non-reference type.)

An expression of value category rvalue may designate a temporary object, or a non-temporary object, or a value of built-in type.

The expressions which create a temporary object all have value category prvalue, however it is then possible to form expressions with category lvalue which designate that same temporary object. For example:

const std::string &v = std::string("hello");

In this case v is an lvalue, but it designates a temporary object. The lifetime of this temporary object matches the lifetime of v, as described in the earlier cppreference link.

Link to further reading about value categories


An rvalue reference is a reference that can only bind to an expression of value category rvalue. (This includes prvalue and xvalue). The word rvalue in its name refers to what it binds to, not its own value category.

All named references in fact have category lvalue. Once bound, there is no difference in behaviour between an rvalue reference and an lvalue reference.

std::string&& rref = std::string("hello");

rref has value category lvalue , and it designates a temporary object. This example is very similar to the previous one, except the temporary object is non-const this time.

Another example:

std::string s1("hello");
std::string&& rref = std::move(s1);
std::string& lref = s1;

In this case, rref is an lvalue, and it designates a non-temporary object. Further, lref and rref (and even s1!) are all indistinguishable from hereon in, except for specifically the result of decltype.

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

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.



Related Topics



Leave a reply



Submit