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?
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
constis perfectly fine. Since it’s okay to assign a reference to the same reference type with a const qualifier.But the second
constis not, as you are literally trying to convert a reference to a vector ofType Ato a reference to a vector ofType B.