Test code snippet below (CHECK
is macro from test framework and works fine):
typedef std::map<int, double> M;
typedef M::value_type VT;
const VT v0;
M mm;
CHECK(mm.begin() == mm.insert(v0).first);
gives false
as result:
ERROR: check is NOT correct CHECK( mm.begin() == mm.insert(v0).first )
values: CHECK( {?} == {?} )
Changing last string to
1)
CHECK(bool(mm.begin() == mm.insert(v0).first));
CHECK(bool(mm.insert(v0).first == mm.begin()));
CHECK(mm.insert(v0).first == mm.begin());
const bool result = mm.insert(v0).first == mm.begin();
CHECK(result);
change nothing: the result is still false
However if I save rvalue of insert result into temporary everything just works:
typedef std::map<int, double> M;
typedef M::value_type VT;
const VT v0;
M mm;
M::iterator it = mm.insert(v0).first;
CHECK(mm.begin() == it);
SUCCESS: check is correct CHECK( mm.begin() == it )
values: CHECK( {?} == {?} )
This behaviour happens only in Release build mode in VS2019 (C++17 or higher) and doesn't happen in any other mode or compiler I've tested.
Could anyone please explain why this is normal or a bug? With C++ standard quotes if necessary.
P.S.: Just to be clear. In my understanding of standard and C++ result is strange because:
std::map::insert
does NOT invalidate any iterators (by standard and MS VS implementation). So call order is not an issue (however I tried all 1-4 possible combinations).bool operator(const std::map::iterator &lhs, const std::map::iterator &rhs)
should prolong lifetime of insertion result rvalue iterator(second) and std::map::end()
rvalue iterator. So comparison should be well defined.As Jarod42 said,
mm.begin()
has unpredictable value.
For different compilers, the results are different:
For issues with different C++ standard running results, you can report the problem to the Developer Community and post a link in the comment so that we can follow up.