Re: [PATCH 3/3] Do not use std::move when assigning an anonymous object to a unique_ptr.

Pedro Alves
2016-11-24T16:52:45Z

On 11/24/2016 12:08 AM, Simon Marchi wrote:
> On 2016-11-23 18:31, John Baldwin wrote:
>> On Wednesday, November 23, 2016 04:19:29 PM Simon Marchi wrote:
>>> On 2016-11-23 15:06, John Baldwin wrote:
>>> > Using std::move forces an extra copy of the object. These changes fix
>>> > -Wpessimizing-move warnings from clang.
>>>
>>> For those who, like me, do not quite understand what is happening here,
>>> I suggest the following read:
>>>
>>> https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en

I'd recommend as well:

https://en.wikipedia.org/wiki/Return_value_optimization
http://en.cppreference.com/w/cpp/language/copy_elision

Note that C++17 has much stronger copy-elision guarantees.

>>>
>>
>> My head also hurts. I think what clang is warning about is that the
>> std::move() in these lines breaks RVO for the function being called,
>> not the function that the modified line belongs to. That is:
>>
>> foo = bar ();
>>
>> Is able to do RVO if bar() does the right things for RVO to work.
>> However:
>>
>> foo = std::move (bar ());
>>
>> forces an extra copy of the object since the return value of bar
>> can't use the storge of 'foo' directly, it has to be copied into
>> an anonymous object (I think) for std::move to consume.
>>
>> The commit log for the warning is here:
>>
>> http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20150427/128053.html
>>
>>
>> I think these instances fall under the "using a move to create a new
>> object
>> from a temporary object" case.
>
> That's what I understand. Without the move, the object is constructed
> directly in the caller's stack, so no move/copy is required at all. It
> seems like the warning works as intended and is useful.

I've been harping on exploring RVO in several patches/reviews,
so I'm surprised I added those std::move calls in the first place. :-P

Maybe something to do with an earlier version of gdb::unique_ptr.

Anyway, removing them is really right thing to do.

Patch is OK, but please drop the leading "gdb/" in filenames
in the ChangeLog.

Thanks,
Pedro Alves