title: std::for_each and std::vector destructor call
body: <p>I have the following problem with std::for_each and a functor proxy object.
  See the following code:</p>
  
  <pre><code>struct Functor {
  
    std::vector&lt;int&gt; data;
  
    const unsigned mID;
    static unsigned id;
  
    Functor() : mID(id++) {
       std::cout &lt;&lt; "Functor constructed with id: " &lt;&lt; mID &lt;&lt; std::endl;
    }
  
    ~Functor() {
      std::cout &lt;&lt; "Functor dtor: " &lt;&lt; mID &lt;&lt; std::endl;
    }
  
     void operator() (int i) {
        std::cout &lt;&lt; "Functor print: " &lt;&lt; i &lt;&lt; std::endl;
        data.push_back(i);
  
        std::cout &lt;&lt; "Dump: ";
        for(int i = 0; i &lt; data.size(); ++i)
          std::cout &lt;&lt; data[i] &lt;&lt; " ";
        std::cout &lt;&lt; std::endl;
     }
  };
  unsigned Functor::id = 0;
  </code></pre>
  
  <p>From above, the proxy object simply does 2 things, it prints the data out to the CMD and it stores a copy for itself to use. Below is the example use case of the object:</p>
  
  <pre><code>int main () {
  
    std::vector&lt;int&gt; intvec;
    for(int i = 0; i &lt; 10; ++i)
      intvec.push_back(i);
  
    Functor myfunctor;
    std::for_each(intvec.begin(), intvec.end(), myfunctor);
  
    std::cout &lt;&lt; "Data in my functor: " &lt;&lt; myfunctor.data.size() &lt;&lt; std::endl;
    for(int i = 0; i &lt; myfunctor.data.size(); ++i)
      std::cout &lt;&lt; "myfunctor data: " &lt;&lt; myfunctor.data[i] &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>This is the part it gets really fishy for me. The output generated is that my functor proxy object is constructed once but deconstructed three times! Something is bypassing the construction call.</p>
  
  <p>Also as a result of the destructor being called at the end of the std::for_each, the Functor.data is empty as well! </p>
  
  <p>Is there a way to make sure data inside Functor is kept persistent? I wish to keep track of the state of my functor when used inside functions such as std::for_each (Basically any given std algorithm function that can take in a unary functor)</p>
  
  <p>Do note that I am using c++03 ONLY. Many thanks.</p>