This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question on move constructors
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Graziano Servizi <Graziano dot Servizi at bo dot infn dot it>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 28 Jan 2013 16:27:50 +0000
- Subject: Re: Question on move constructors
- References: <5106A25D.1070805@bo.infn.it>
On 28 January 2013 16:07, Graziano Servizi wrote:
> Thanks for your kind and quick answer.
>
> Indeed now the output from sample_code agrees (except minor changes) with
> the one given.
>
> I had to give, however, the noexcept specifier not only to the "move"
> functions, but to the copy constructor and copy assignment operator as well.
That should not be necessary, and is wrong because the copy
constructor allocates memory and so can throw exceptions.
>
> Trying the same on my own class was unsuccessful: I have a simple, ordinary,
> class, say "class my_class", with several constructors,
> the copy and move constructors and the move and copy = operators.
>
> ALL of them are NOW noexcept specified.
>
> Nevertheless when I write
>
> my_class my_object(MY_FUNCTION());
>
> where MY_FUNCTION() is a function returning a my_class object,
> neither the copy constructor nor the move constructor is called: is this due
> to RETURN VALUE OPTIMIZATION? Doesn't MY_FUNCTION() is an rvalue?
Yes, this is the return value optimisation.
To disable it you can compile with -fno-elide-constructors and you
will see every copy/move happen, without them being elided.
> Must I "noexcept" EVERY method of my_class?
No.