title: const arguments in std::remove_if
body: <p>I'm going to remove elements from a <strong>list of pairs</strong>. When I'm using a pair like </p>
  
  <p><code>std::pair&lt;const int, bool&gt;</code> </p>
  
  <p>I get the following compilation error: </p>
  
  <blockquote>
    <p>In file included from /usr/local/include/c++/6.1.0/utility:70:0,</p>
    
    <p>from /usr/local/include/c++/6.1.0/algorithm:60,</p>
    
    <p>from main.cpp:1:</p>
    
    <p>/usr/local/include/c++/6.1.0/bits/stl_pair.h: In instantiation of
    'std::pair&lt;_T1, _T2>&amp; std::pair&lt;_T1, _T2>::operator=(std::pair&lt;_T1,
    _T2>&amp;&amp;) [with _T1 = const int; _T2 = bool]':</p>
    
    <p>/usr/local/include/c++/6.1.0/bits/stl_algo.h:868:16:   required from
    '_ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator,
    _Predicate) [with _ForwardIterator = std::_List_iterator > _Predicate =
    __gnu_cxx::__ops::_Iter_pred&amp;)> >]'</p>
    
    <p>/usr/local/include/c++/6.1.0/bits/stl_algo.h:936:30:   required from
    '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter =
    std::_List_iterator > _Predicate =
    main()::&amp;)>]'</p>
    
    <p>main.cpp:17:32:   required from here</p>
    
    <p>/usr/local/include/c++/6.1.0/bits/stl_pair.h:319:8: error: assignment
    of read-only member 'std::pair::first'</p>
    
    <p>first = std::forward(__p.first);</p>
  </blockquote>
  
  <p>This is the sample code:</p>
  
  <pre><code>int main()
  {
      int id = 2;
  
      std::list&lt; std::pair &lt;const int, bool&gt; &gt;  l;
      l.push_back(std::make_pair(3,true));
      l.push_back(std::make_pair(2,false));
      l.push_back(std::make_pair(1,true));
  
      l.erase(std::remove_if(l.begin(), l.end(), 
          [id](std::pair&lt;const int, bool&gt;&amp; e) -&gt; bool {
          return e.first == id; }));
  
      for (auto i: l) {
          std::cout &lt;&lt; i.first &lt;&lt; " " &lt;&lt; i.second &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>I know that (please correct me If I am wrong): </p>
  
  <ol>
  <li><p>I will have exactly the same problem as long as there is constness in any element of the list, for example, a <code>list &lt;const int&gt;</code> will also return a compilation error.</p></li>
  <li><p>If I remove the const in the first element of the pair the code will work.</p></li>
  <li><p>The more elegant and efficient way to do it is by using the remove_if list method, like this: </p>
  
  <pre><code>l.remove_if([id](std::pair&lt;const int, bool&gt;&amp; e) -&gt; bool {
      return e.first == id; });
  </code></pre></li>
  </ol>
  
  <p>but my question is, what are exactly the inner workings of std::remove_if that impose the elements of the container not being <em>const</em>? </p>