The meaning of the iterator passed as a position hint to std::set::insert(iterator position, const value_type& val)
and std::multiset::insert(iterator position, const value_type& val)
changes between C++98 and C++ 11. Is there an easy way at compile-time to detect which is in use and use different code?
A general check on C++11 does not appear to be a good idea (1, 2), and I didn't see a suitable Boost.Config macro.
Specifically, the documentation for C++98 says:
The function optimizes its insertion time if position points to the element that will precede the inserted element.
while for C++11 it says:
The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).
This matters because the hint effects the complexity of the insertion call. If the hint is correct, the complexity is only an amortized constant. But if it's not, it's logarithmic in size.
As so nicely described below by JerryCoffin, the C++98 specification is, essentially, a typo.
This was the subject of a defect report, LWG issue #233, and n1780.
As noted there, it was apparently a simple error in editing the C++98 standard.
Stepanov's original implementation worked according to the current specification, and as far as I know most (all?) implementations since have done the same (though there is one that looks both before and after the hinted location, so you'll get optimal behavior if you specify either one).
This also makes it possible to hint that an insertion will be at the beginning of the set, which wasn't possible under the previous specification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With