Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #128

Re: noexcept-specification vs returning a prvalue

Message-ID <4db5b2ce$0$6764$9b4e6d93@newsspool3.arcor-online.net> (permalink)
Newsgroups comp.std.c++
From Johannes Schaub <schaub.johannes@googlemail.com>
Followup-To comp.std.c++
Subject Re: noexcept-specification vs returning a prvalue
Organization Arcor
References <83f4aba6-91b5-4c95-b298-46cc269651ee@r23g2000vbp.googlegroups.com>
Date 2011-04-26 11:04 -0600

Followups directed to: comp.std.c++

Show all headers | View raw


(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                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Find similar


Thread

noexcept-specification vs returning a prvalue  Nikolay Ivchenkov <tsoae@mail.ru> - 2011-04-23 10:21 -0600
  Re: noexcept-specification vs returning a prvalue  Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-04-26 11:05 -0600
    Re: noexcept-specification vs returning a prvalue Johannes Schaub<schaub.johannes@googlemail.com> - 2011-04-27 10:51 -0600
  Re: noexcept-specification vs returning a prvalue  Johannes Schaub <schaub.johannes@googlemail.com> - 2011-04-26 11:04 -0600

csiph-web