To prevent the data from being modified, include const
in the template parameter for your pointer:
std::vector<std::unique_ptr<const Foo>> vec;
I think you'll have problems making the pointer itself const
though. The reason is that the vector has to be able to copy the pointer object around internally (e.g. when the container is resized). With a unique_ptr
, that means ownership has to be transferred between instances, meaning it has to be mutable (not const).
To make the pointer itself const
, I think you've got two main choices: use a vector of const shared_ptr<>
, or an array (i.e. fixed size) of const unique_ptr<>
.