Topic: Defect Report: 12.2 implies static object destroyed before temporary


Author: Allan Odgaard <Duff@DIKU.DK>
Date: Sun, 22 Feb 2004 17:50:07 +0000 (UTC)
Raw View
[note: Forwarded to C++ Committee. -sdc ]

Section 12.2 paragraph 5 ends with this "rule":

    [...] if obj2 is an object with static or automatic
    storage  duration created  after  the temporary  is
    created,  the temporary  shall  be destroyed  after
    obj2 is destroyed.

For the temporary to be destoyed after obj2 is destroyed, when obj2 has
static storage, I would say that the reference to the temporary should
also have static storage, but that is IMHO not clear from the
paragraph.

Example:

    void f ()
    {
       const T1& ref = T1();
       static T2 obj2;
       ...
    }

Here the temporary would be destoyed *before* obj2, contrary to the
rule above.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net (James Kuyper)
Date: Mon, 23 Feb 2004 11:26:19 CST
Raw View
Allan Odgaard <Duff@DIKU.DK> wrote in message news:<3E980EBA-6459-11D8-85B0-00039351BBEE@Top-House.DK>...
> [note: Forwarded to C++ Committee. -sdc ]
>
> Section 12.2 paragraph 5 ends with this "rule":
>
>     [...] if obj2 is an object with static or automatic
>     storage  duration created  after  the temporary  is
>     created,  the temporary  shall  be destroyed  after
>     obj2 is destroyed.
>
> For the temporary to be destoyed after obj2 is destroyed, when obj2 has
> static storage, I would say that the reference to the temporary should
> also have static storage, but that is IMHO not clear from the
> paragraph.

There's no such requirement, unless the temporary was created by the
declaration of another static object.

> Example:
>
>     void f ()
>     {
>        const T1& ref = T1();
>        static T2 obj2;
>        ...
>     }
>
> Here the temporary would be destoyed *before* obj2, contrary to the
> rule above.

The rule above only applies when the temporary is created before the
static object. If T2 is a POD type, then obj2 would be
zero-initialized before any non-static object is initialized.
Therefore, I'll assume you're thinking of T2 as a non-POD type, in
which case initialization can be put off until the first time that
control passes through the declaration of obj2. It doesn't have to be,
but it can.

If obj2 gets created after the construction of the temporary, how
would you conclude that the temporary will be destroyed in conflict
with 12.2p5? Is there some other part of the standard that requires
12.2p5 to be destroyed earlier tha obj2? If so, which one, and why?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]