
Boost : 
Subject: Re: [boost] [operators] What is the correct overload set for a binary operator?
From: Daniel Frey (d.frey_at_[hidden])
Date: 20130428 15:15:55
On 28.04.2013, at 17:52, Marc Glisse <marc.glisse_at_[hidden]> wrote:
> Splitting this into const& and && overloads of lhs would save a move for:
> T r = std::move(a) + b;
> (passing an xvalue and not a prvalue)
Thanks Marc, it seems that we still need the usual 4 overloads on const lvalue reference and rvalue reference for the parameters and just return an rvalue in all cases. With todays compilers I fail to find a case for passbyvalue. Currently, this works best for me:
T operator+( const T& lhs, const T& rhs )
{
T nrv( lhs );
nrv += rhs;
return nrv;
}
T operator+( const T& lhs, T&& rhs )
{
#ifdef COMMUTATIVE
rhs += lhs;
return std::move( rhs );
#else
T nrv( lhs );
nrv += std::move( rhs );
return nrv;
#endif
}
T operator+( T&& lhs, const T& rhs )
{
lhs += rhs;
return std::move( lhs );
}
T operator+( T&& lhs, T&& rhs )
{
lhs += std::move( rhs );
return std::move( lhs );
}
I'll write a larger test program when I have some more time which allows better comparison of different overload sets and we can then try to come up with expressions to show the benefits and the drawbacks of each technique, including if possible cases that benefit from passbyvalue.
BR, Daniel
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk