public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/102704] NRVO for throw expression
Date: Wed, 13 Oct 2021 08:43:56 +0000	[thread overview]
Message-ID: <bug-102704-4-pB4hy0PnDe@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-102704-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102704

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-10-13

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
[class.copy.elision] p1:
This elision of copy/move operations, called copy elision, is permitted in the
following circumstances (which may be combined to eliminate multiple copies):
-- in a return statement [...]
-- in a throw-expression (7.6.18), when the operand is the name of a
non-volatile object with automatic storage duration (other than a function or
catch-clause parameter) that belongs to a scope that does not contain the
innermost enclosing compound-statement associated with a try-block (if there is
one), the copy/move operation can be omitted by constructing the object
directly into the exception object
-- in a coroutine [...]
-- when the exception-declaration of an exception handler [...]


The slightly confusing "does not contain the innermost enclosing
compound-statement" means you can't elide if the throw is in a try-block and
the variable was defined outside it.

So this cannot be elided:

void f()
{
  mytype x;
  try {
    throw x;
  } catch (int) {
  }
}

But this can be:

void g()
{
  try {
    mytype x;
    throw x;
  } catch (int) {
  }
}

And the example in comment 0 can be.

  parent reply	other threads:[~2021-10-13  8:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-12 13:39 [Bug c++/102704] New: " vanyacpp at gmail dot com
2021-10-13  6:22 ` [Bug c++/102704] " rguenth at gcc dot gnu.org
2021-10-13  8:43 ` redi at gcc dot gnu.org [this message]
2021-10-13  8:47 ` pinskia at gcc dot gnu.org

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=bug-102704-4-pB4hy0PnDe@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).