c++ - const correctness for containers -
after years of blindly accepting fact std::vector<t>::operator[] const returns const_reference, but, in light of how const works smart pointers, i'm beginning wonder why , rest of stl containers designed way. seems "constness" of const std::vector being applied both vector , elements, whereas smart pointers "constness" applies pointer , not element it's pointing.
to clarify, seems there should vector-like container const means user can't change size of container, elements in container mutable. main question is: there prevent type of container being "const correct"?
it seems there couple of hackish workarounds adding layer of indirection (e.g. std::vector<std::unique_ptr<t>> const) accomplish this, i'm looking little less awkward in terms of maintenance.
as aside, if smart pointers incorporated language before stl containers, const accessors still have been defined way today?
to clarify, seems there should vector-like container const means user can't change size of container, elements in container mutable.
that's std::array. set size @ compile time. setting size @ constructor-time there's proposed dynarray.
Comments
Post a Comment