title: C++: STL: set: stored value constness
body: <p>Having the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  #include &lt;string&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  class Employee {
    // ...
    int _id;
    string _name;
    string _title;
  public:
    Employee(int id): _id(id) {}
  
    string const &amp;name() const { return _name; }
    void setName(string const &amp;newName) { _name = newName; }
  
    string const &amp;title() const { return _title; }
    void setTitle(string const &amp;newTitle) { _title = newTitle; }
  
    int id() const { return _id; }
  };
  
  struct compEmployeesByID: public binary_function&lt;Employee, Employee, bool&gt; {
    bool operator()(Employee const &amp;lhs, Employee const &amp;rhs) {
      return lhs.id() &lt; rhs.id();
    }
  };
  
  int wmain() {
    Employee emplArr[] = {0, 1, 2, 3, 4};
    set&lt;Employee, compEmployeesByID&gt; employees(emplArr, emplArr + sizeof emplArr/sizeof emplArr[0]);
    // ...
    set&lt;Employee, compEmployeesByID&gt;::iterator iter = employees.find(2);
    if (iter != employees.end())
      iter-&gt;setTitle("Supervisor");
  
    return 0;
  }
  </code></pre>
  
  <p>I cannot compile this code having (MSVCPP 11.0):</p>
  
  <pre><code>1&gt;  main.cpp
  1&gt;d:\docs\programming\test01\test01\main.cpp(40): error C2662: 'Employee::setTitle' : cannot convert 'this' pointer from 'const Employee' to 'Employee &amp;'
  1&gt;          Conversion loses qualifiers
  </code></pre>
  
  <p>This helps to compile:</p>
  
  <pre><code>  if (iter != employees.end())
      const_cast&lt;Employee &amp;&gt;(*iter).setTitle("Supervisor");
  </code></pre>
  
  <p>The question: I know that <code>map</code> and <code>multimap</code> store their values as <code>pair(const K, V)</code> where K is a key and V is a value. We cannot change the K object. But <code>set&lt;T&gt;</code> and <code>multiset&lt;T&gt;</code> store their object as <code>T</code>, not as <code>const T</code>. So WHY I NEED THIS CONST CAST??</p>