Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-04-13 02:18:46


Thorsten Ottosen wrote:

> I think I have found a safe way to
> bind a temporary to a const reference and cast away the constness.

   template<šclassšTš>
   voidšis_lvalue_cast_safe(šTš)š
   {}

   template<šclassšTš>
   voidšis_lvalue_cast_safe(šT&š)
   {}

So, if you pass non-const rvalue the second overload can't be used and
everything's OK. When passing const rvalue T will be "const" in the second
overload and you get ambiguity. That's cute.

I wonder if all current compilers are smart enough to not call copy
constructor when passing parameter? At least all gcc versions I have,
starting with 2.95 avoid the copy.

When looking at:
my_copy( my_sort( BOOST_LVALUE( my_unique( BOOST_LVALUE( vec() ) ) ) ),
ostream_iterator<int>( cout ) );

I wish we can get away from BOOST_LVALUE too. The simple approach of:

   template<class T>
   void sort(T t) {}

   template<class T>
   void sort(T& t) {}

does not really work, since there's ambiguity when passing non-const
reference. It's probably possible to use mojo to distinguish non-const
lvalues, const objects and non-const temporary and then add compile-time
fail inside version which is selected for const objects. But we can't
change definition of std::vector :-(

- Volodya


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