Boost logo

Boost :

Subject: Re: [boost] [operators] The future
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-10-04 18:26:38


on Tue Oct 04 2011, GMan <gmannickg-AT-gmail.com> wrote:

> On Tue, Oct 4, 2011 at 8:38 AM, Matt Calabrese <rivorus_at_[hidden]> wrote:
>
>> type operator +( type const&, type const& ) or an equivalent with copies is
>> defined automatically as normal
>>
>> type operator +( type&&, type const& ) is defined to use += on the first
>> argument which is then move-returned
>>
>> type operator +( type const& left, type&& right ) is defined as return
>> std::move( right ) + left
>>
>> type operator +( type&& left, type&& right ) required for disambiguation
>> and
>> just returns std::move( left ) + right
>>
>
> If we're going for C++11, I think the correct way to do this is with a
> single function:
>
> type operator+(type lhs, const type& rhs)
> {
> lhs += rhs;
> return lhs;
> }
>
> That should, given that the class implements move-semantics, be sufficient.

With that implementation

    x + (y + z)

will make unnecessary copies.

This is a solved problem. You need 4 overloads to handle all the cases
without ambiguity. See the rvalue reference proposals for detailed
examples.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk