C++ Logo

std-discussion

Advanced search

Re: Storage duration of temporary objects

From: Brian Bi <bbi5291_at_[hidden]>
Date: Sun, 7 Dec 2025 10:37:57 -0500
On Sat, Dec 6, 2025 at 11:36 PM Yongwei Wu via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Under [basic.stc.general] <https://eel.is/c++draft/basic.stc#general>,
> clause 1 writes:
>
> The *storage duration*
>> <https://eel.is/c++draft/basic.stc.general#def:storage_duration> is the
>> property of an object that defines the minimum potential lifetime of the
>> storage containing the object. The storage duration is determined by the
>> construct used to create the object and is one of the following [four
>> categories omitted].
>
>
> So it apparently covers all kinds of objects, including temporary objects.
>

> Although clause 2 mentions temporary objects (an improvement over
> published standards), none of the categories seems to fit temporaries.
> Where [class.temporary] <https://eel.is/c++draft/class.temporary> mentions
> storage durations, it is about the destruction order. E.g.:
>
> if obj1 is an object with the same storage duration as the temporary and
> created before the temporary is created the temporary shall be destroyed
> before obj1 is destroyed
>
>
> The good side is that it strengthens the idea that a temporary object
> should have a storage duration, and even that it should be one of the
> defined storage durations. However, if the temporary is not
> lifetime-extended, the automatic storage duration does not really fit—
> [basic.stc.auto] <https://eel.is/c++draft/basic.stc.auto> only describes
> variables and scopes (as vs unnamed objects and full-expressions).
>

We already treat objects with automatic storage duration slightly
differently depending on whether they're block variables or function
parameters. We could potentially bring temporary objects (that are not
lifetime-extended) within the same umbrella. Maybe that would simplify
[class.temporary], or maybe not. Someone needs to volunteer for drafting.


> So my feeling is that the standard is already trying to reconcile an
> inconsistency in its language, but has not yet fully done so.
>

The CWG issue is: CWG1634 <https://cplusplus.github.io/CWG/issues/1634.html>


>
> Is my understanding correct? Or did I miss anything?
>
> --
> Yongwei Wu
> URL: http://wyw.dcweb.cn/
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>


-- 
*Brian Bi*

Received on 2025-12-07 15:38:12