It seems that I'm missing something obvious, but I can't find an elegant solution of the following problem.
I need to create a collection of objects, where some of them are pointed by shared_ptr (and thus owned by container itself) and some by weak_ptr (thus not owned).
What is the best way of doing this? So far I came up with rather ugly solution like this:
template <class Data>
struct holder {
shared_ptr<Data> owned;
weak_ptr<Data> borrowed;
shared_ptr<Data> get(){
if(owned)
return owned;
else
return borrowed.lock();
}
}
...
vector<holder<MyData>> vec;
I can overload assignment for shared_ptr and weak_ptr and also dereferencing to make it "transparent" to store both kind of things but all this looks complex and non-optimal.
Is there any better way of doing this?