Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. for_each calls copy ctor on functor?

for_each calls copy ctor on functor?

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsalgorithmshelpquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Malcolm Smart
    wrote on last edited by
    #1

    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
    dtor

    Regards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.

    S A 2 Replies Last reply
    0
    • M Malcolm Smart

      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
      dtor

      Regards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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'

      1 Reply Last reply
      0
      • M Malcolm Smart

        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
        dtor

        Regards Angel ********************************************* The sooner you fall behind, the longer you have to catch up.

        A Offline
        A Offline
        Andrew Walker
        wrote on last edited by
        #3

        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;
        }
        

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups