NuTo
Numerics Tool
`boost::ptr_container` vs. `std::container` of `std::unique_ptr`

Note: If you need shared semantics, neither is the right solution. Use a std::container of std::shared_ptrs

Advantages of boost::ptr_container

Value semantics:

1 boost::ptr_vector<MyClass> vec;
2 vec.push_back(new MyClass);
3 vec[0].foo();

vs. pointer semantics with unique pointers

1 std::vector<std::unique_ptr<MyClass>> vec;
2 vec.push_back(std::make_unique<MyClass>());
3 vec[0]->foo();

Propagates constness

1 void f(const boost::ptr_vector<MyClass> vec)
2 {
3  vec[0].SetMember(42.0); // Compile time error
4 }

vs.

1 void g(const std::vector<std::unique_ptr<MyClass>>& vec)
2 {
3  vec[0]->SetMember(42.0); // will compile and run
4 }

Built-in support for deep copy semantics

For a std::container of unique pointers, you would have to implement a deep copy yourself. In Boost, it is built in, using the Clonable Concept

Lower memory footprint and usually faster

See Boost Ptr Container Documentation.

Advantages of std::container<std::unique_ptr<MyClass>>

No shallow copy possible

You can't make a shallow copy, because the unique_ptr is not copy constructible. This is good. With boost pointer containers, you could have copy of the container with dangling pointers after the original container went out of scope.

Don't make shallow copies of owning containers, regardless of how they are implemented.