I can't find any similar questions. The moment I call a getMap, the previously iterator seems to change:
//IF I COMMENT THE EVIL PRINT, THE PROBLEM DOES NOT OCCUR std::cout << "EVIL PRINT" << std::endl; Something something; auto mapElement = something.getTheMap().find("A"); std::cout << "Before: " << mapElement->first << std::endl; something.getTheMap(); std::cout << "After: " << mapElement->first << std::endl << std::endl; /****************************************************************************************/ //WITH SHARED POINTERS, THE EVIL PRINT IS NOT NECCESARY TO MAKE THE PROBLEM OCCUR std::shared_ptr<Something> somePtr; auto mapElement2 = something.getTheMap().find("A"); std::cout << "Before: " << mapElement2->first << std::endl; something.getTheMap(); std::cout << "After: " << mapElement2->first << std::endl << std::endl; OUTPUT:
EVIL PRINT Before: A After: B Before: A After: B The complete code is runnable here https://coliru.stacked-crooked.com/a/66b48636a476ddb7
Is this a wanted behaviour? What is happening?