for_each calls copy ctor on functor?
-
This is simplified version of what I am doing but still shows the issue : The Copy Ctor is being called by the for_each algorithm. Whilst in this case it isn't an issue, but in my live app it is. Why is the copy ctor being called and can I prevent it..?
class ProcessVector
{
public:
ProcessVector(const ProcessVector &rhs)
{
cout << "copy ctor" << endl;
}
ProcessVector()
{
cout << "ctor" << endl;
}
~ProcessVector()
{
cout << "dtor" << endl;
}
void operator ()(int i)
{
cout << i << endl;
}};
int main(int argc, char* argv[])
{
std::vector <int> test;
for (int i = 0 ; i< 5; i++)
test.push_back(i);for\_each(test.begin(), test.end() , ProcessVector() ); return 0;
}
gives the following output
ctor
0
1
2
3
4
copy ctor
dtor
dtorRegards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.
-
This is simplified version of what I am doing but still shows the issue : The Copy Ctor is being called by the for_each algorithm. Whilst in this case it isn't an issue, but in my live app it is. Why is the copy ctor being called and can I prevent it..?
class ProcessVector
{
public:
ProcessVector(const ProcessVector &rhs)
{
cout << "copy ctor" << endl;
}
ProcessVector()
{
cout << "ctor" << endl;
}
~ProcessVector()
{
cout << "dtor" << endl;
}
void operator ()(int i)
{
cout << i << endl;
}};
int main(int argc, char* argv[])
{
std::vector <int> test;
for (int i = 0 ; i< 5; i++)
test.push_back(i);for\_each(test.begin(), test.end() , ProcessVector() ); return 0;
}
gives the following output
ctor
0
1
2
3
4
copy ctor
dtor
dtorRegards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.
It's by design - the C++ Standard specifies that the function object is passed by value, not by reference and that a *copy* of the function object is returned by
for_each
. You'll have to work round whatever problems this causes you. Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p' -
This is simplified version of what I am doing but still shows the issue : The Copy Ctor is being called by the for_each algorithm. Whilst in this case it isn't an issue, but in my live app it is. Why is the copy ctor being called and can I prevent it..?
class ProcessVector
{
public:
ProcessVector(const ProcessVector &rhs)
{
cout << "copy ctor" << endl;
}
ProcessVector()
{
cout << "ctor" << endl;
}
~ProcessVector()
{
cout << "dtor" << endl;
}
void operator ()(int i)
{
cout << i << endl;
}};
int main(int argc, char* argv[])
{
std::vector <int> test;
for (int i = 0 ; i< 5; i++)
test.push_back(i);for\_each(test.begin(), test.end() , ProcessVector() ); return 0;
}
gives the following output
ctor
0
1
2
3
4
copy ctor
dtor
dtorRegards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.
Easy, add an extra level of redirection to manage the lifetime. This issue is described as Stateful Predicates in the book Exceptional C++
#include <algorithm> #include <vector> #include <iostream> using namespace std; class ProcessVector { public: ProcessVector() { cout << "ctor" << endl; } ~ProcessVector() { cout << "dtor" << endl; } void operator ()(int i) { cout << i << endl; } ProcessVector(const ProcessVector &rhs) { cout << "copy ctor" << endl; } }; class ProcessVectorIndirect { public: ProcessVectorIndirect(ProcessVector* process) : pProcess(process) { } void operator ()(int i) { (*pProcess)(i); } private: ProcessVector* pProcess; }; int main(int argc, char* argv[]) { std::vector <int> test; for (int i = 0 ; i< 5; i++) test.push_back(i); ProcessVector vect; for_each(test.begin(), test.end() , ProcessVectorIndirect(&vect) ); return 0; }