Topic: noexcept-specification vs returning a prvalue


Author: Nikolay Ivchenkov <tsoae@mail.ru>
Date: Sat, 23 Apr 2011 10:21:44 CST
Raw View
Consider the following example:

     #include <iostream>

     struct C
     {
         C();
         C(C const &) noexcept(false);
     };

     C f() noexcept(true);

     int main()
     {
         std::cout << (int)noexcept(f());
     }

Here f is declared with non-throwing exception-specification, though
copy constructor of its return type does not have non-throwing
exception-specification. What is the value of expression noexcept(f())?


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 26 Apr 2011 11:05:03 CST
Raw View
On 2011-04-23 18:21, Nikolay Ivchenkov wrote:
>
> Consider the following example:
>
>      #include<iostream>
>
>      struct C
>      {
>          C();
>          C(C const&) noexcept(false);
>      };
>
>      C f() noexcept(true);
>
>      int main()
>      {
>          std::cout<<  (int)noexcept(f());
>      }
>
> Here f is declared with non-throwing exception-specification, though
> copy constructor of its return type does not have non-throwing
> exception-specification. What is the value of expression noexcept(f())?

I think the FDIS says that noexcept(f()) shall evaluate to "true" in
this example. My interpretation is based on the way how I read the
meaning of "A function is said to allow" in [except.spec] p. 8
(complemented by p. 12) to refer to exceptions that are possibly
invoked by the function implementation including its return statement.

The return statement includes the construction and possible move/copy
of the return value of f() as explained in [stmt.return] p. 2:

"A return statement can involve the construction and copy or move of a
temporary object (12.2)."

so the tagging of f with noexcept(true) means that we are sure that
the function call including the construction and possible copy [move
is not possible here] of the return value) does no throw an exception.
It seems to me that this does not include the destruction of the
temporary, so extending the definition of C like

      struct C
      {
          C();
          C(C const&) noexcept(false);
          ~C noexcept(false);
      };

should change the value of noexcept(f()) to false, because the
noexcept operator also considers the destruction of the temporary of
the complete function call expression.

It could be helpful, if [except.spec] p. 8 and/or p. 12 would be
clearer in this regard.

HTH & Greetings from Bremen,

Daniel Kr   gler




--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Tue, 26 Apr 2011 11:04:50 CST
Raw View
(second submission attempt after 48h).

Nikolay Ivchenkov wrote:

>
> Consider the following example:
>
>      #include <iostream>
>
>      struct C
>      {
>          C();
>          C(C const &) noexcept(false);
>      };
>
>      C f() noexcept(true);
>
>      int main()
>      {
>          std::cout << (int)noexcept(f());
>      }
>
> Here f is declared with non-throwing exception-specification, though
> copy constructor of its return type does not have non-throwing
> exception-specification. What is the value of expression noexcept(f())?
>

The value is "true", and calling "f" results in a call to std::terminate if
the copy consructor of C actually throws.

I would say the author of "f" needs to make sure its return type either is
nothrow-copyable, or that all constructions it does by returning will not
throw.

In addition, this is not tied to the copy constructor, I think. The
following would be fine if "f"'s author knows that C's default construtor
would no throw, too:

 C f() noexcept(true) { return {}; /* note: no copy! */ }

Note that any implicit call to the copy constructor of "C" for a return
statement has to occur in the context of the caller. So to the noexcept
operator, it does not make a difference whether or not the copy constructor
has a throwing exception spec.

If C has a potentially throwing dtor, that makes a difference though,
because the dtor is invoked in the context of the caller (the noexcept test-
expression).


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Johannes Schaub<schaub.johannes@googlemail.com>
Date: Wed, 27 Apr 2011 10:51:47 CST
Raw View
Daniel Kr  gler wrote:

>
>  On 2011-04-23 18:21, Nikolay Ivchenkov wrote:
>>
[snipped, to avoid overquoting].
>>
>>  Here f is declared with non-throwing exception-specification, though
>>  copy constructor of its return type does not have non-throwing
>>  exception-specification. What is the value of expression noexcept(f())?
>
>  I think the FDIS says that noexcept(f()) shall evaluate to "true" in
>  this example. My interpretation is based on the way how I read the
>  meaning of "A function is said to allow" in [except.spec] p. 8
>  (complemented by p. 12) to refer to exceptions that are possibly
>  invoked by the function implementation including its return statement.
>
[...]
>
>  It could be helpful, if [except.spec] p. 8 and/or p. 12 would be
>  clearer in this regard.
>

The unary "noexcept" operator is specified at 5.3.7[expr.unary.noexcept],
not at [except.spec].


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]