title: C++ - why does the [] operator extend a map
body: <p>This is nothing new (except for me): when using the [] operator on a const map like</p>
  
  <pre><code>int get_number_map(const string&amp; s, const map&lt;string, int&gt;&amp; book)
  {
      return book[s];
  } 
  </code></pre>
  
  <p>you'll a compile-time error. </p>
  
  <p>There are plenty of answers out there for the question "Why will my code not compile?", e.g. <a href="https://stackoverflow.com/a/28419540/2179994">here</a>. You should do <code>return book.at(s);</code> (C++11 or later) or use the <code>.find()</code> method.</p>
  
  <p>Why is [] designed to work like this on maps? To me this is a side effect that breaks the consistency of the usage of [].</p>