public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xinliang David Li <davidxl@google.com>
To: Olivier Ballereau <Olivier.Ballereau@gmx.net>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
	Jason Merrill <jason@redhat.com>,
		Richard Guenther <richard.guenther@gmail.com>
Subject: Re: New option to turn off stack reuse for temporaries
Date: Mon, 03 Dec 2012 01:03:00 -0000	[thread overview]
Message-ID: <CAAkRFZ+6tfQxBaNi=98MDC9e7yZ9Cxc6aRfzzWKKVViq5ZaZGg@mail.gmail.com> (raw)
In-Reply-To: <50BB4A2D.5050408@gmx.net>

My first example is not correct --- according to the standard, the
lifetime of the temporary should be extended. The second example is an
user error.

 C++ standard says this in 12.2.5:

"
The second context is when a reference is bound to a temporary. The
temporary to which the reference is
bound or the temporary that is the complete object of a subobject to
which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s
ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call
(5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a
function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression
in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4)
persists until the completion of the
full-expression containing the new-initializer. [Example:
struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} }; // Creates dangling reference
— end example ] [ Note: This may introduce a dangling reference, and
implementations are encouraged
to issue a warning in such a case. — end note ]
"

David




On Sun, Dec 2, 2012 at 4:31 AM, Olivier Ballereau
<Olivier.Ballereau@gmx.net> wrote:
> Hello David,
>
> Sorry to come so late into the discussion, but...
>
> On 21/06/12 00:50, Xinliang David Li wrote:
>> One of the most common runtime errors we have seen in gcc-4_7 is
>> caused by dangling references to temporaries whole life time have
>> ended
>>
>> e.g,
>>
>>  const A& a = foo();
>>
>> or
>> foo (A());// where temp's address is saved and used after foo.
>>
>> Of course this is user error according to the standard,
>> [...]
>
> ... is the first of your 2 examples really a user error? If so, it
> breaks GotW #88: A Candidate For the “Most Important const” [1]. Can you
> please clarify?
>
> Thanks in advance!
> Olivier
>
> [1]
> http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/
>

  reply	other threads:[~2012-12-03  1:03 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-20 23:44 Xinliang David Li
2012-06-21  5:28 ` Jason Merrill
2012-06-21  6:06   ` Xinliang David Li
2012-06-21  6:27     ` Jason Merrill
2012-06-21  9:32     ` Richard Guenther
2012-06-21 16:41       ` Michael Matz
2012-06-22  8:46         ` Richard Guenther
2012-06-21 18:19       ` Jason Merrill
2012-06-21 18:44       ` Xinliang David Li
2012-06-22  8:50         ` Richard Guenther
2012-06-22  9:39           ` Jason Merrill
2012-06-22  9:51             ` Richard Guenther
2012-06-22 16:09               ` Xinliang David Li
2012-06-25 16:29                 ` Xinliang David Li
2012-06-26  8:42                   ` Richard Guenther
2012-06-26 15:29                     ` Jason Merrill
2012-06-26 17:12                       ` Michael Matz
2012-06-26 17:19                         ` Jakub Jelinek
2012-06-26 20:12                         ` Mike Stump
2012-06-27  3:03                           ` Eric Botcazou
2012-06-29  8:18                     ` Xinliang David Li
2012-07-02 23:30                       ` Xinliang David Li
2012-07-04 15:01                         ` Xinliang David Li
2012-07-09 16:31                           ` Xinliang David Li
2012-07-09 22:53                         ` Jason Merrill
2012-12-02 12:32 ` Olivier Ballereau
2012-12-03  1:03   ` Xinliang David Li [this message]
2012-06-22 21:09 Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAkRFZ+6tfQxBaNi=98MDC9e7yZ9Cxc6aRfzzWKKVViq5ZaZGg@mail.gmail.com' \
    --to=davidxl@google.com \
    --cc=Olivier.Ballereau@gmx.net \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).