#include <iostream>
#include <algorithm>
struct functor {
~functor() {std::cout << 'D';}
void operator()(int a) {std::cout << a;}
};
int main()
{
int a[] = {1,2,3,4,5};
functor f;
std::for_each(a ,a+5,f);
}
// produces : 12345DDD
notice the three D's in the output.
The functor is copied once as pass-by-value to for_each()
and once again as the return value from for_each().
Is there a way to use for_each without introducing these
extra copies of the functor? Perhaps this is just a
g++ implementation issue?
Regards,
Sean
#include <algorithm>
struct functor {
~functor() {std::cout << 'D';}
void operator()(int a) {std::cout << a;}
};
int main()
{
int a[] = {1,2,3,4,5};
functor f;
std::for_each(a ,a+5,f);
}
// produces : 12345DDD
notice the three D's in the output.
The functor is copied once as pass-by-value to for_each()
and once again as the return value from for_each().
Is there a way to use for_each without introducing these
extra copies of the functor? Perhaps this is just a
g++ implementation issue?
Regards,
Sean
Comment