title: Why can't a struct defined inside a function be used as functor to std::for_each?
body: <p>The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so?</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;algorithm&gt;
  
  struct Element
  {
      void flip() {}
  };
  
  void flip_all(std::map&lt;Element*, Element*&gt; input)
  {
      struct FlipFunctor
      {
          void operator() (std::pair&lt;Element* const, Element*&gt;&amp; item)
          {
              item.second-&gt;flip();
          }
      };
  
      std::for_each(input.begin(), input.end(), FlipFunctor());
  }
  </code></pre>
  
  <p>When I move <code>struct FlipFunctor</code> before function <code>flip_all</code>, the code compiles.</p>
  
  <p>Full error message:</p>
  
  <blockquote>
    <p>no matching function for call to ‘for_each(std::_Rb_tree_iterator&lt;std::pair&lt;Element* const, Element*&gt; &gt;, std::_Rb_tree_iterator&lt;std::pair&lt;Element* const, Element*&gt; &gt;, flip_all(std::map&lt;Element*, Element*, std::less&lt;Element*&gt;, std::allocator&lt;std::pair&lt;Element* const, Element*&gt; &gt; &gt;)::FlipFunctor)’</p>
  </blockquote>