C++ Logo

std-discussion

Advanced search

Re: Is the standard clear enough about throwing exceptions from functions that return prvalues?

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Fri, 20 Sep 2019 20:53:50 +0300
On Fri, 20 Sep 2019 at 20:19, Andrew Schepler via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> By my reading, foo should catch the exception, so gcc and clang are correct. The key quote is [stmt.return]/2: When the returned initializer is not an expression with type void,
>
> the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.
>
>
> So even though the prvalue result object is s in main, the initialization and the exception throwing happen in foo.
>
> I suppose this means we should think of prvalue temporary materialization as only a piece of semantic analysis of a class type expression in its context, and not something that gets "executed" by the virtual machine.

To be sure, the exceptions, if any, thrown by the initialization of
the returned value should always be catchable by foo, no matter
whether elision
happens or whether there's some other funny language-level
optimization business going on.

Received on 2019-09-20 12:56:10