Why using iterators this way works?

Viewed 248

Here is a minimal code in order to recreate the condition that makes me doubt:

#include <map>
#include <string>

int main()
{
  std::map<std::string, std::string> mm;

  mm.emplace("Hi", "asd");
  mm.emplace("Hey", "asd");
  mm.emplace("Hello", "asd");

  std::map<std::string, std::string>::const_iterator it = mm.find("Hey");
  it->second.size();

  // A
  //it->second.replace(0,1,"h");

  //B
  auto u = it->second;
  u.replace(0,1,"h");
}

Why is there a error of passing constant as an argument in case A but works in case B ?

Details of the error:

error: passing 'const std::basic_string' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]' discards qualifiers [-fpermissive]

1 Answers
Related