Clone wiki

value_ptr / Home


value_ptr is a so-called smart pointer. Really, it wants to be a smart reference, but you can't overload the "." operator in C++, so "->" had to do. Because the use of the -> operator makes it feel like you're using a pointer, it is named as such. I'm not really happy with the name. If you can think of a better one, answers on a post card please...

In short, when a value_ptr is copied, it performs a "deep copy" of the pointee. There are lots of existing smart pointers designed for this purpose, but I found them lacking in some respect. Most notably value_ptr has the following features:

  • it is about as efficient as this kind of smart pointer can be…
  • … while still being exception safe
  • can interact with existing polymorphic hierarchies that provide a clone()-like function (though the member function doesn’t have to be called clone())
  • provides conversions to/from value_ptrs referring to objects of related types e.g. a value_ptr<derived> can be assigned to a value_ptr<base>
  • does not allow slicing under any circumstances
  • entirely non-intrusive
  • support for custom allocators
  • written in portable C++

The motivation for this kind of thing can be found elsewhere.


The short version

Download value_ptr.hpp and #include it! Comments abound in the header file.