I have a collection of unique_ptr
s. Here i want to take some of them and return them to the caller. The caller only needs to read the content, so i wanted to use constant references. But I'm not sure how to do this with unique_ptr
s.
Here is some code I used to do this with raw pointers:
class entry
{
};
vector<entry*> master;
const vector<entry*> get_entries()
{
vector<entry*> entries;
// peusocode, master contains all entries.
// only some entries are copied, filtered by some predicate
copy_if(master.begin(), master.end(), back_inserter(entries), ...);
}
How do I do this with unique_ptr
s? I could also use shared_ptr
, but the ownership is quite clear and as I mentioned the caller does not need write access.