Can functors be defined locally within the body of a function?
One possible use for locally defined functors I can think of, (especially if we want to use STL algorithms), is say
we have two vectors std::vector<int>
's a
and b
then we can consider them in equal in many ways. i.e. a[i] = b[i] (mod loop_counter)
where the loop_counter keeps changing and we test them for equality in every loop iteration.
for(int i=0 ; i<=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
}
If the answer is no, how would one do the above, where the condition for equality keeps changing with every iteration?
NOTE: I tried defining a functor as follows (no for
loops here) but the program failed to compile.
#include <algorithm>
#include <iostream>
#include <list>
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 << "Sum of evens: " << evenodd.even_sum() << "\n";
std::cout << "Sum of odds: " << evenodd.odd_sum() << std::endl;
// output:
// Sum of evens: 30
// Sum of odds: 25
}
On moving the functor definition before main()
the code compiled cleanly, and executed correctly.
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.