Consider this code
#include
#include
const int& foo(const std::vector& x,unsigned i) {
auto it = x.be
The problem is that the value_type of std::mapstd::pair, but std::pair. Then for return *it;, a temporary std::pair has to be created and returned. (std::pair could be converted from std::pair.) The temporary will be destroyed immediately and left the returned reference dangled.
To sovle the issue you can change the return type to const std::pair or const std::map.