Re: Question on move constructors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux