I was reading the documentation of std::sub_match<BidirectionalIterator>
and saw that it publicly inherits from std::pair<BidirectionalIterator, BidirectionalIterator>
. Since a sub_match
is simply a pair of iterators into a sequence of characters, with some additional functions, I can understand that it is implemented with a pair
, but why use public inheritance?
The problem with inheriting publicly from std::pair<T,U>
is the same as inheriting publicly from most other standard classes: they are not meant to be manipulated polymorphically (notably they do not define a virtual destructor). Other members will also fail to work properly, namely the assignment operator and the swap member function (they will not copy the matched
member of sub_match
).
Why did Boost developers and then the committee decided to implement sub_match
by inheriting publicly from pair
instead of using composition (or private inheritance with using declarations if they wanted to keep member access through first
and second
)?