title: Does std::map require the comparator's operator() to be const?
body: <p>The following code fails to compile with XCode 4.5's clang++ when using libc++ on OS X 10.8:</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;string&gt;
  
  class Foo {
  public:
    explicit Foo(int val_) : val(val_) {}
    int val;
  };
  
  struct FooComparator {
    bool operator()(const Foo&amp; left, const Foo&amp; right) {
      return left.val &lt; right.val;
    }
  };
  
  int main(int argc, char* argv[]) {
  
    std::map&lt;Foo, std::string, FooComparator&gt; m;
  
    Foo f(4);
    m[f] = std::string("four");
  
    return 0;
  }
  </code></pre>
  
  <p>The error:</p>
  
  <blockquote>
    <p>broken.cpp:11:8: note: candidate function not viable: 'this' argument
    has type 'const FooComparator', but
          method is not marked const   bool operator()(const Foo&amp; left, const Foo&amp; right) {</p>
  </blockquote>
  
  <p>If I turn off libc++ and build with libstdc++ then all is well. Obviously, I can work around this by making FooComparator::operator() const, but I'd like to understand whether this is a problem with libc++ being too strict, or whether the standard (both C++03 and C++11) does in fact require that the comparator's operator() be const (in which case the fact that it works with libstdc++ is a happy accident).</p>