|
Boost : |
From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-06-28 09:29:33
Hi Daniel,
From: "Daniel Frey" <daniel.frey_at_[hidden]>
> 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?
Actually, I'm not up on the NRVO rules, but I should be. From looking at
the csc++ thread John Potter seems to be arguing with you, which is never a
good sign for your side of the argument <wink>, but for all I know you've
got everything worked out correctly.
What I'd like to see is a condensed (1-2 paragraphs) analysis of the
problem and your solution with references to the standard so I can quickly
crosscheck it. If it looks good to everyone here I'll ask you for a patch.
How does that sound?
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk