Given the following class based on containers of shared pointers,
class Foo;
class Bar {
public:
// ...
const std::vector<boost::shared_ptr<const Foo> >& getFoos() const { return foos_; }
private:
std::vector<boost::shared_ptr<Foo> > foos_;
};
which will not compile because
invalid initialization of reference of type ‘const std::vector<boost::shared_ptr<const Foo>, std::allocator<boost::shared_ptr<const Foo> > >&’ from expression of type ‘const std::vector<boost::shared_ptr<Foo>, std::allocator<boost::shared_ptr<Foo> > >’
The foos_
member needs to point to mutable Foo
objects for internal use by the Bar
object, but I don't want client code calling getFoos()
to be able to modify anything.
Removing the const
qualifier from Foo
in the getFoos()
return type fixes this. However, I understand that while std::vector
propagates its constness to its elements, boost::shared_ptr
does no such thing to the object it points to (naturally). Thus, it seems to me getFoos()
no longer observes its const
qualifier (even though the compiler doesn't complain), because client code can modify the Foo
objects pointed to by the shared pointers returned.
Am I correct? If so, is there some way to write getFoos()
so that it will return a const vector of const references to const objects without copying?
You can return a
const shared_ptr<Foo>*
pointer instead of a vector, and use explicit ugly C-style casting to convertfoos_.data()
into whatever you want.Since you're interested in returning a const vector, you don't lose much by returning a pointer, except sizing information, of course. You can always wrap the new pointer in a class designed to provide this sizing information, as supplied at moment of its creation.
I could be wrong but I really don't think you can achieve this.
shared_ptr<Foo>
can become ashared_ptr<const Foo>
only through a construction of a new instance toshared_ptr<const Foo>
A reference to
shared_ptr<Foo>
cannot become a reference toshared_ptr<const Foo>
, simply because they are two different types.Here you are trying to get a reference to
vector<shared_ptr<Foo>>
into the form ofconst vector<shared_ptr<const Foo>>
.The first
const
is perfectly fine. Since it's okay to assign a reference to the same reference type with a const qualifier.But the second
const
is not, as you are literally trying to convert a reference to a vector ofType A
to a reference to a vector ofType B
.Instead of returning a
std::vector<...> const&
, what about returning a range? A range is apair
of iterators of some kind. In this case, your iterators will be tostd::shared_ptr<const foo>
. You can do this by writing up a quick iterator adapter that internally iterates overconst_iterator
tostd::shared_ptr<foo>
, but returns them asstd::shared_ptr<const foo>
.Most operations you'll want to perform on a
const
vector
can be performed on arange
of random-accessconst_iterator
s.