Return a const vector of const shared pointers to

2019-06-25 21:06发布

问题:

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?

回答1:

I could be wrong but I really don't think you can achieve this.

shared_ptr<Foo> can become a shared_ptr<const Foo> only through a construction of a new instance to shared_ptr<const Foo>

A reference to shared_ptr<Foo> cannot become a reference to shared_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 of const 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 of Type A to a reference to a vector of Type B.



回答2:

Instead of returning a std::vector<...> const&, what about returning a range? A range is a pair of iterators of some kind. In this case, your iterators will be to std::shared_ptr<const foo>. You can do this by writing up a quick iterator adapter that internally iterates over const_iterator to std::shared_ptr<foo>, but returns them as std::shared_ptr<const foo>.

Most operations you'll want to perform on a const vector can be performed on a range of random-access const_iterators.



回答3:

You can return a const shared_ptr<Foo>* pointer instead of a vector, and use explicit ugly C-style casting to convert foos_.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.