Boost logo

Boost :

From: Andrei Alexandrescu (andrewalex_at_[hidden])
Date: 2002-10-22 14:33:41


"Daniel Frey" <d.frey_at_[hidden]> wrote in message
news:ap470c$u81$1_at_main.gmane.org...
> URVO is preferable, yes. It works almost everywhere, but sometimes - like
> for operator+ - the URVO can't help, you need the NRVO.

Why can't it help?

T operator+(const T& lhs, const T& rhs)
{
    return T(lhs) += rhs;
}

> In these cases I
> think it's essential to help the compiler:
>
> T f()
> {
> T nrv;
> // add your code here ...
> return nrv;
> }

But an explicit move constructor such as:

T f()
{
    T nrv;
    // add your code here ...
    return T(nrv, move);
}

/always/ transforms an lvalue into a temporary at a small cost. So the idiom
above works efficiently on compilers that do URVO or NRVO, while your idiom
works only on compilers that support NRVO (which I understand are a subset
of the former).

So I'd say for programmers who want to make their code efficient on more
platforms, the latter idiom is better. The disadvantage, of course, is that
T needs to implement that constructor.

Andrei


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