Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2000-10-10 15:58:20


"Erdei, András" wrote on 10/10/2000 4:33 PM
>and found operator++ implemented as
>
>// incrementable and decrementable contributed by Jeremy Siek
>
> friend T operator++(T& x, int)
> {
> incrementable_type tmp(x);
> ++x;
> return tmp;
> }
>
>a more efficient implemetation can be (stripped off
>the template stuff):
>
> class proxy
> {
> public :
> operator T const & ( void ) { return real ; }
> ~proxy() { if ( ! std::uncaught_exception() ) ++real ; } ;
> private :
> friend proxy operator++( T & , int ) ;
> proxy( T & real ) : real( real ) {} ;
> T & real ;
> } ;
>
> proxy
>operator++( T & t , int )
> {
> return proxy( t ) ;
> }

I haven't coded it up and tried it, but I'm concerned that an iterator
implemented this way might fail with the following code:

MyIterator i, j, k;
...
std::copy(i++, j, k); // compile-time error?

-Howard


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