|
Boost : |
From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-07-17 04:52:22
Eric Niebler wrote:
> (Some fancy auto_ptr-like move shenanigans are needed to make the lock
> return-able but not copy-able.)
I think there's a better way to express types that are movable but not
copyable, which could work on more compilers.
struct lock;
lock try_lock();
template<class T, class U> struct enable_move {};
template<class T>
struct enable_move<T, T const>
{
typedef typename T::error_cant_move_from_const type;
};
template<class T>
struct enable_move<T, T>
{
typedef int type;
};
struct lock
{
lock(lock const& other)
{
// move the lock from const_cast<lock&>(other)
}
lock& operator=(lock const& other)
{
// move the lock from const_cast<lock&>(other)
}
operator bool() const
{
// TODO return true iff we're holding the lock
return true;
}
private:
friend lock try_lock();
lock(){} // a try-lock c'tor
// make this type non-copyable, non-assignable
template<class T>
lock(T&, typename enable_move<lock, T>::type = 0);
template<class T>
typename enable_move<lock, T>::type operator=(T&);
};
inline lock try_lock()
{
// call a special try-lock c'tor on lock
return lock();
}
int main()
{
if( lock l = try_lock() )
{
}
if ( lock const& l = try_lock() ) // doesn't compile on comeau with
// the auto_ptr ref thing
{
}
}
-- Daniel Wallin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk