title: Polymorphic functors in std::for_each
body: <p>I'm trying to use stl algorithm for_each without proliferating templates throughout my code. std::for_each wants to instantiate MyFunctor class by value, but it can't since its abstract. I've created a functor adapter class which passes a pointer around and then derefernces it when appropriate.</p>
  
  <p>My Question: </p>
  
  <p>Does the STL or Boost already have such an adapter class? I don't want to have to reinvent the wheel!</p>
  
  <pre><code> struct MyFunctor  {
       virtual ~MyFunctor() {}
       virtual void operator()(int a) = 0; 
   }
  
   namespace {
       template&lt;typename FunctorType, typename OperandType&gt; struct
   FunctorAdapter
       {
           FunctorAdapter(FunctorType* functor) : mFunctor(functor) {}
           void operator()(OperandType&amp; subject)
           {
               (*mFunctor)(subject);
           }
  
           FunctorType* mFunctor;
       }; }
  
   void applyToAll(MyFunctor &amp;f) {
       FunctorHelper&lt;MyFunctor, int&gt; tmp(&amp;f);
       std::for_each(myvector.begin(), myvector.end(), tmp); }
  </code></pre>
  
  <p>Cheers,</p>
  
  <p>Dave </p>