Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2004-01-12 09:48:44


On Jan 12, 2004, at 4:46 AM, Daniel Frey wrote:

> X operator+( const X& lhs, const X& rhs ) {
> X nrv( lhs );
> nrv += rhs;
> return nrv;
> }
>
> X operator-( X lhs, const X& rhs ) {
> lhs += rhs;
> return lhs;
> }

Nice example, I see what you mean. I hadn't thought about this case.
The main characteristic between this and the case I was thinking about
is the need to return the modified value. IIUC, this characteristic
disables NRVO.

Fwiw, here is the case I was thinking about (and assumed Andrei was
too):

#include <iostream>

struct X {
   X() {}
   X( const X& ) { std::cout << "X(X)\n"; }
   void swap(X&) {}
};

template <class T>
void
strong_assign1(T& lhs, const T& rhs)
{
        T(rhs).swap(lhs);
}

template <class T>
void
strong_assign2(T& lhs, T rhs)
{
        rhs.swap(lhs);
}

int main()
{
        X x1, x2;
        strong_assign1(x1, x2);
        std::cout << "---\n";
        strong_assign1(x1, X());
        std::cout << "---\n";
        strong_assign2(x1, x2);
        std::cout << "---\n";
        strong_assign2(x1, X());
        std::cout << "---\n";
}

X(X)

---
X(X)
---
X(X)
---
---
-Howard

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