I am confused why the following code compiles.
#include <iostream>
#include <map>
int main()
{
std::map<int,int, std::less<int>> m {{1,10}, {2,20}};
std::map<int,int, std::greater<int>>::const_iterator it = m.begin();
std::cout << it->second << std::endl;
it++;
std::cout << it->second << std::endl;
}
I assumed C++ would be "strongly typed" in this regard, and disallow me to assign to an iterator of another map type.
I compiled with C++23 switch, but standard version does not seem to affect it.
note: I presume map iterator does not need to know about comparison since it just needs to know how to go to previous/next and dereference, but still seems weird this compiles.
edit: I see const_iterator is "type erasing" but I would like to know why.
cpp reference:
const_iterator LegacyBidirectionalIterator to const value_type