Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-01-18 18:16:56


"Daniel Wallin" <dalwan01_at_[hidden]> wrote in message
news:400B0CA9.4050209_at_student.umu.se...
> Howard Hinnant wrote:
> > <snip>
> >
> > Below is a brief, realistic demo of the dangers of moving with
copy
> > syntax from an lvalue:
> >
> ><snip>
> >
> > It is safe to move from lvalues with some syntax other than copy.
So
> > the functionality of auto_ptr can still be had, just not with the
same
> > syntax. You might consider something like:
> >
> > http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/
> > n1377.htm#move_ptr%20Example
> >
> > This example relies on a language feature not available to you.
> > However, I believe you can emulate this behavior in today's C++
using
> > the same techniques you have demonstrated.
>
> How about:
>
> template<class T>
> class move_ptr
> {
> public:
> move_ptr() : m_p(0) {}
>
> template<class U>
> move_ptr(const move_ptr<U>& p)
> : m_p(const_cast<move_ptr<U>&>(p).release())
> {}
>
> explicit move_ptr(T* p) : m_p(p)
> {}
>
> ~move_ptr()
> {
> delete m_p;
> }
>
> move_ptr& operator=(move_ptr p)
> {
> p.swap(*this);
> return *this;
> }
>
> void reset(T* p)
> {
> move_ptr(p).swap(*this);
> }
>
> T* release()
> {
> T* p = m_p;
> m_p = 0;
> return p;
> }
>
> void swap(move_ptr& p)
> {
> std::swap(m_p, p.m_p);
> }
>
> T& operator*() const
> {
> return *m_p;
> }
>
> T* operator->() const
> {
> return m_p;
> }
>
> private:
> template<class U> struct cant_move_from_const;
> template<class U> struct cant_move_from_const<const
move_ptr<U> >
> {
> typedef typename move_ptr<U>::error type;
> };
>
> template<class U>
> move_ptr(U&, typename cant_move_from_const<U>::type = 0);
>
> move_ptr(move_ptr&);
> template<class U> move_ptr(move_ptr<U>&);
>
> T* m_p;
> };
>

I was going to try this later today. You beat me to it!

It looks good on VC7.1 and Intel7.1 (Windows). Codewarrior 8.0 can't
handle it, as expected.

Jonathan


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