This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Question on move constructors


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]