Let
class A { std::vector<std::shared_ptr<int>> v_; };
Now I would like to add access to v_ using two public member functions
std::vector<std::shared_ptr<int>> const & v() { return v_; }
and
std::vector<std::shared_ptr<int const> const & v() const { TODO }
I can not replace TODO with return v_; .
One option is not to return the link, but copy. Besides the obvious performance degradation, this will also make the interface less desirable.
Another option is to make TODO equal to return reinterpret_cast<std::vector<std::shared_ptr<int const>> const &>(v_);
My question is: is this behavior undefined? Or, alternatively, is there a better option, preferably without using reinterpret_cast ?
c ++ undefined-behavior vector reinterpret-cast
Xoph
source share