Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-06-11 04:04:29


Richard Peters wrote:
>
> Then you can replace the postfix increment operator with
>
> number operator++(int)
> { return postfix_increment(*this); }
>
> The only thing is that I do not clearly see if there's still the copying
> advantage...
> Is this useful for the utility library?

I think it depends on the compiler. Compared to the canonical

number operator++( int )
{ number tmp( *this ); ++( *this ); return tmp; }

you may get a performance advantage for compiler without the "named
return value optimization". But this is basically a problem of the
compiler and I would like to see some evidence that there are compilers
that do benefit from this. For compilers without good optimizers, it may
even lead to slower code as it is more complicated. A possible solution
might be a #define:

#ifdef BOOST_COMPILER_LIKES_POSTFIX_INCREMENT_OPTIMIZATION

template< class T > struct postfix_incrementer : public T
{
   postfix_incrementer( T& rhs ) : T( rhs )
   {
      ++rhs;
   }
};

template< class T > T postfix_increment( T& rhs )
{
   return postfix_incrementer< T >( rhs );
}

#define BOOST_RETURN_POSTFIX_INCREMENT( T ) return postfix_increment(
*this )

#else // BOOST_COMPILER_LIKES_POSTFIX_INCREMENT_OPTIMIZATION

#define BOOST_RETURN_POSTFIX_INCREMENT( T ) T tmp( *this ); ++( *this );
return tmp

#endif // BOOST_COMPILER_LIKES_POSTFIX_INCREMENT_OPTIMIZATION

Now implement operator++( int ) like this:

number operator++( int )
{ BOOST_RETURN_POSTFIX_INCREMENT( number ); }

All we need to find out is: which compilers should
BOOST_COMPILER_LIKES_POSTFIX_INCREMENT_OPTIMIZATION be set for...

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