I have a few containers in a class, for example, vector or map which contain
shared_ptr's to objects living on the heap.
For example
template <typename T>
class MyExample
{
public:
private:
vector<shared_ptr<T> > vec_;
map<shared_ptr<T>, int> map_;
};
I want to have a public interface of this class that sometimes returns shared_ptrs to const objects (via shared_ptr<const T>
) and sometimes shared_ptr<T>
where I allow the caller to mutate the objects.
I want logical const correctness, so if I mark a method as const, it cannot change the objects on the heap.
Questions:
1) I am confused by the interchangeability of shared_ptr<const T>
and shared_ptr<T>
. When someone passes a shared_ptr<const T>
into the class, do I:
- Store it as a
shared_ptr<T>
orshared_ptr<const T>
inside the container? - OR
- Do I change the map, vector types (e.g. insert_element(
shared_ptr<const T>
obj)?
2) Is it better to instantiate classes as follows: MyExample<const int>
? That seems unduly restrictive, because I can never return a shared_ptr<int>
?
Best Answer
shared_ptr<T>
andshared_ptr<const T>
are not interchangable. It goes one way -shared_ptr<T>
is convertable toshared_ptr<const T>
but not the reverse.Observe:
compile via
You can also overload a function based on a constness. You can combine to do these two facts to do what you want.
As for your second question,
MyExample<int>
probably makes more sense thanMyExample<const int>
.