Defining functor within the body of a code
<p>Can functors be defined locally within the body of a function?</p>

<p>One possible use for locally defined functors I can think of, (especially if we want to use STL algorithms), is say </p>

<p>we have two vectors <code>std::vector&lt;int&gt;</code>'s <code>a</code> and <code>b</code> then we can consider them in equal in many ways. i.e. <code>a[i] = b[i] (mod loop_counter)</code> where the loop_counter keeps changing and we test them for equality in every loop iteration. </p>

<pre><code>for(int i=0 ; i&lt;=10 ; ++i)
{
//Define binary predicate functor my_local_predicate

//Test for equality using functor local to loop
std::equal(a.begin(), a.end(), b.begin, my_local_predicate)

 // Do something if they are equal OR unequal

}
</code></pre>

<p>If the answer is no, how would one do the above, where the condition for equality keeps changing with every iteration? </p>

<p><strong>NOTE:</strong> I tried defining a functor as follows (no <code>for</code> loops here) but the program failed to compile. </p>

<pre><code>#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;



int main() {

     class EvenOddFunctor 
  {
    int even_;
    int odd_;
    public:
    EvenOddFunctor() : even_(0), odd_(0) {}
    void operator()(int x) {
        if (x%2 == 0) even_ += x;
        else odd_ += x;
    }
    int even_sum() const { return even_; }
    int odd_sum() const { return odd_; }
  };

    EvenOddFunctor evenodd;

    int my_list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    evenodd = std::for_each(my_list,
                  my_list+sizeof(my_list)/sizeof(my_list[0]),
                  evenodd);

    std::cout &lt;&lt; "Sum of evens: " &lt;&lt; evenodd.even_sum() &lt;&lt; "\n";
    std::cout &lt;&lt; "Sum of odds: " &lt;&lt; evenodd.odd_sum() &lt;&lt; std::endl;

    // output:
    // Sum of evens: 30
    // Sum of odds: 25
}
</code></pre>

<p>On moving the functor definition before <code>main()</code> the code compiled cleanly, and executed correctly. </p>

<p>So even if defining a functor within a body of a function seems impossible, I would like some nice STL like way of changing  the equality condition at every iteration. </p>
