简体   繁体   中英

How to ensure constness of the returning vector<unique_ptr>

I asked related question here . Now it is a little subtler.

Here is the code:

class MyClass {
  public:
    const vector<unique_ptr<MyObject> >& get_const_objs() const;

  private:
    vector<unique_ptr<MyObject>> m_objs;
};

My intention is that the returned vector from get_const_objs() is read-only, but the problem is because the elements of the vector are not const, so caller still can change the individual elements, eg

const vector<unique_ptr<MyObject>>& objs = pMyClass->get_const_objs();
unique_ptr<MyObject> p = move(objs[0]);

My solution is to insert const to the vector:

const vector<const unique_ptr<MyObject> >& get_const_objs() const;

But this leads to a boring implementation of get_const_objs() which I copy each element to a new vector:

const vector<const unique_ptr<MyObjects>>& MyClass::get_const_objs() const
{
  vector<const unique_ptr<MyObjects>> ret;
  for (const auto &obj : my_objs) {
    ret.push_back(obj);
  }
  return ret;
}

Yes, I can add iterator interface to MyClass. Is there any other solution?

I have a restriction: BOOST is not available. But I like to know BOOST solution if really there is good one just using standard.

A (little) better solution is to return std::vector<const MyObject*> and not expose std::unique_ptr .

std::vector<const MyObject*> get_const_objs() const
{
    std::vector<const MyObject*> res;
    res.reserve(my_objs.size());
    for (const auto& obj : my_objs) {
        res.push_back(obj.get());
    }
    return res;
}

Instead of recreating the vector each time, you may have this vector as member, but then you need to keep the vectors synchronized.

const vector<unique_ptr<MyObject>>& objs = pMyClass->get_const_objs();
unique_ptr<MyObject> p = move(objs[0]);

You cannot do it , So you don't have to worry!

Since objs is const vector<> & , the element of this vector is also treated as const . Therefore, you cannot "move" it; can you move const objects?

Custom iterators seem to be the easiest way to go, simply expose only an iterator dereferencing to const MyObject&

class ConstObjectIter {
public:
    ...
    const MyObject& operator* () const { return **m_it; }
    const MyObject* operator->() const { return &**this; }
    ConstIter& operator++() { ++m_it; return *this; }
    ...
private:
    std::vector<std::unique_ptr<MyObject> >::const_iterator m_it, m_end;
}

initialise m_it, m_end with m_objs.begin(), m_objs.end() resp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM