Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-06-28 02:01:59


Hi,

I noticed that boost libraries sometimes implement some operators in a
way that prevents optimizations. Example from boost/operators.hpp:

template <class T, class U, class B = ::boost::detail::empty_base>
struct multipliable2 : B
{
     friend T operator*(T x, const U& y) { return x *= y; }
     friend T operator*(const U& y, T x) { return x *= y; }
};

This works, but it prevents the NRVO (named return value optimization).
An implemenation that solves this is:

template <class T, class U, class B = ::boost::detail::empty_base>
struct multipliable2 : B
{
     friend T operator*(const T& x, const U& y)
       { T nrv( x ); nrv *= y; return nrv; }

     friend T operator*(const U& y, const T& x)
       { T nrv( x ); nrv *= y; return nrv; }
};

For further details, see also the thread "Temporaries and optimization?"
in csc++. (A message not yet approved explains why the name should be
'nrv', it should show up on csc++ in the next hours).

Or is there anything I missed?

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de


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