Page about vtkSmartPointer

// vtkNew, in contrast to vtkSmartPointer, has no assignment operator
// or copy constructor and owns one object for its whole lifetime.
// Thus vtkNew does not satisfy the CopyAssignable and CopyConstructible
// requirements needed for other std containers like std::vector or std::list.
// std::array, on the other hand, is a container encapsulating fixed size
// arrays so its elements do not need to be CopyAssignable and
// CopyConstructible.

Good news, this is no longer true – all of the VTK smart pointers, including vtkNew are usable with std::vector.

In C++11, the wording of std::vector<T>'s restrictions on the T type changed to remove the bits about CopyAssignable and CopyConstructible. The only remaining hard restriction is Erasable, any other requirements depend on how the container is actually used. While there are a few methods that explicitly require T to be copyable (e.g. std::vector::resize(size_type, const T&)), most only require that T is MoveInsertible.

All of the VTK smart pointers are MoveInsertible as of this commit and can be used in a std::vector, and vtkNew<T> can now be treated like any other move-only container. This came up a while ago in a post that discussed the changes. If you read that link, note that while you can return a vtkNew from a function, it’s easy to misuse since the issue mentioned in that thread was never resolved.

2 Likes