I know that it's a terrible idea to change the key of an object in an associative container, but I wonder where exactly the standard forbids me to do so. Consider:
#include <map>
#include <memory>
struct X { int i; };
struct lt
{
bool operator()( const std::shared_ptr< X >& lhs,
const std::shared_ptr< X >& rhs ) const
{
return lhs->i < rhs->i;
}
};
int main()
{
std::map< std::shared_ptr< X >, int, lt > m;
auto x = std::make_shared< X >();
x->i = 1;
m.insert( std::make_pair( x, 2 ) );
x->i = 42; // change key wrt the container!
}
I assume that the above should be illegal, but I was reading the standard for some time now and I can't find anything that actually makes it illegal. Where is it? Or is it hiding in a future defect report?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…