Topic: A new smart pointer with move semantics ?


Author: loufoque <loufoque@remove.gmail.com>
Date: Fri, 14 Jul 2006 14:19:19 CST
Raw View
auto_ptr's copy constructor actually moves the object rather than
copying it.
With move semantics introduced through rvalue references in C++0x, it
becomes possible to use the move constructor to move instead, and why
not use the copy constructor to do an actual deep copy, possibly virtual.

Introducing a new smart pointer like this would not only supplant
auto_ptr but also make a wrapper around pointers to base classes,
creating objects that behave like normal while having polymorph features.
It could also be used to move objects that aren't movable or where
moving is rather expensive.

It seems to me that kind of smart pointer would be very useful as it
addresses a number of problems related to memory management and
polymorphism and thus should be in the next standard library.
Are there any plans for such a thing ?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Joe Gottman" <jgottman@carolina.rr.com>
Date: Sat, 15 Jul 2006 01:19:46 CST
Raw View
"loufoque" <loufoque@remove.gmail.com> wrote in message
news:44b2b6d0$0$31660$626a54ce@news.free.fr...
> auto_ptr's copy constructor actually moves the object rather than copying
> it.
> With move semantics introduced through rvalue references in C++0x, it
> becomes possible to use the move constructor to move instead, and why not
> use the copy constructor to do an actual deep copy, possibly virtual.
>
> Introducing a new smart pointer like this would not only supplant auto_ptr
> but also make a wrapper around pointers to base classes, creating objects
> that behave like normal while having polymorph features.
> It could also be used to move objects that aren't movable or where moving
> is rather expensive.
>
> It seems to me that kind of smart pointer would be very useful as it
> addresses a number of problems related to memory management and
> polymorphism and thus should be in the next standard library.
> Are there any plans for such a thing ?

   It's already been proposed.  It's called unique_ptr, and you can read
about it here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1856.html

Joe Gottman

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Sat, 15 Jul 2006 07:39:25 CST
Raw View
In article <44b2b6d0$0$31660$626a54ce@news.free.fr>,
 loufoque <loufoque@remove.gmail.com> wrote:

> auto_ptr's copy constructor actually moves the object rather than
> copying it.
> With move semantics introduced through rvalue references in C++0x, it
> becomes possible to use the move constructor to move instead, and why
> not use the copy constructor to do an actual deep copy, possibly virtual.
>
> Introducing a new smart pointer like this would not only supplant
> auto_ptr but also make a wrapper around pointers to base classes,
> creating objects that behave like normal while having polymorph features.
> It could also be used to move objects that aren't movable or where
> moving is rather expensive.
>
> It seems to me that kind of smart pointer would be very useful as it
> addresses a number of problems related to memory management and
> polymorphism and thus should be in the next standard library.
> Are there any plans for such a thing ?

Well, there is the unique_ptr proposal:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1856.html#Additi
on%20-%20Class%20template%20unqiue_ptr

I'm not sure if that covers what you're suggesting though.  One could
also use a clone_ptr, which could be both cheaply movable, and implement
a virtual copy construction on copy.  clone_ptr hasn't been proposed,
but there are noises on boost.

-Howard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Abrahams <dave@boost-consulting.com>
Date: Sat, 15 Jul 2006 13:09:18 CST
Raw View
loufoque <loufoque@remove.gmail.com> writes:

> auto_ptr's copy constructor actually moves the object rather than
> copying it.
> With move semantics introduced through rvalue references in C++0x, it
> becomes possible to use the move constructor to move instead, and why
> not use the copy constructor to do an actual deep copy, possibly
> virtual.
>
> Introducing a new smart pointer like this would not only supplant
> auto_ptr but also make a wrapper around pointers to base classes,
> creating objects that behave like normal while having polymorph
> features.  It could also be used to move objects that aren't movable
> or where moving is rather expensive.
>
> It seems to me that kind of smart pointer would be very useful as it
> addresses a number of problems related to memory management and
> polymorphism and thus should be in the next standard library.
> Are there any plans for such a thing ?

You can download one that works today:
http://home.comcast.net/~jturkanis/move_ptr/

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]