Question on move constructor

Jonathan Wakely jwakely.gcc@gmail.com
Mon Jan 28 15:14:00 GMT 2013


On 28 January 2013 14:46, Jonathan Wakely wrote:
> On 28 January 2013 14:35, Graziano Servizi wrote:
>>
>> WHEN are they called?
>
> This is not the right list for answering that, as it's not a question about GCC.
>
> They're called when constructing from an rvalue.
>
>> I got an example on Internet (attached) and the output they claim to be
>> obtained (also attached) is NOT reproduced on my system (it is attached as
>> well).
>>
>> I also tried some checks of my own and I was unable to call a move
>> constructor that I wrote in my classes (I presume in a correct way, since
>> the compiler didn't complain). It seems to me that, anytime the standard
>> 2011 claims that a move constructor should be called, the "classical" copy
>> constructor is called instead, unless one explicitly use a call to the
>> std::move function. Is this correct or wrong?
>
> I can't comment on your own tests, but in the sample_code.C program
> you attached the std::vector operations do not move the elements
> because your move constructor and move assignment operator can throw
> exceptions.

To be more precise, the library code cannot prove they will not throw
exceptions, so it assumes they might throw.

To inform the library they will not throw, add the "noexcept"
specifier to the move functions, so that std::vector knows it is safe
to move the objects.

>  The std::vector implementation in libstdc++ will only
> move elements if doing so is guaranteed not to throw exceptions,
> otherwise there could be data loss and the vector contents could be
> left in an inconsistent state.
>
> To ensure your class gets moved, make your move operations "noexcept"



More information about the Gcc-help mailing list