title: How to achieve "virtual template function" in C++
body: <p>first off: I have read and I know now that a virtual template member function is not (yet?) possible in C++. A workaround would be to make the class a template and then use the template-argument also in the member-function.</p>
  
  <p>But in the context of OOP, I find that the below example would not be very "natural" if the class was actually a template. Please note that the code is actually not working, but the gcc-4.3.4 reports: <code>error: templates may not be ‘virtual’</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class Animal {
      public:
          template&lt; class AMOUNT &gt;
          virtual void eat( AMOUNT amount ) const { 
              std::cout &lt;&lt; "I eat like a generic Animal." &lt;&lt; std::endl; 
          }
          virtual ~Animal() { 
          }
  };
  
  class Wolf : public Animal {
      public:
          template&lt; class AMOUNT &gt;
          void eat( AMOUNT amount) const { 
              std::cout &lt;&lt; "I eat like a wolf!" &lt;&lt; std::endl; 
          }
          virtual ~Wolf() { 
          }
  };
  
  class Fish : public Animal {
      public:
          template&lt; class AMOUNT &gt;
          void eat( AMOUNT amount) const { 
              std::cout &lt;&lt; "I eat like a fish!" &lt;&lt; std::endl; 
          }
          virtual ~Fish() { 
          }
  };
  
  class GoldFish : public Fish {
      public:
          template&lt; class AMOUNT &gt;
          void eat( AMOUNT amount) const { 
              std::cout &lt;&lt; "I eat like a goldfish!" &lt;&lt; std::endl; 
          }
          virtual ~GoldFish() { 
          }
  };
  
  class OtherAnimal : public Animal {
          virtual ~OtherAnimal() { 
          }
  };
  
  int main() {
      std::vector&lt;Animal*&gt; animals;
      animals.push_back(new Animal());
      animals.push_back(new Wolf());
      animals.push_back(new Fish());
      animals.push_back(new GoldFish());
      animals.push_back(new OtherAnimal());
  
      for (std::vector&lt;Animal*&gt;::const_iterator it = animals.begin(); it != animals.end(); ++it) {
          (*it)-&gt;eat();
          delete *it;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>So creating a "Fish&lt; Amount > foo" is kind of strange. However, it seems desirable to me to provide an arbitrary amount of food to eat for each animal.</p>
  
  <p>Thus, I am searching a solution about how to achieve something like</p>
  
  <pre><code>Fish bar;
  bar.eat( SomeAmount food );
  </code></pre>
  
  <p>This becomes particularly useful when looking at the for-loop. One might like to feed a specific amount (FoodAmount) to all of the different animals (via eat() and bind1st() e.g.), it could not be done that easily, although I wound find this very inuitive (and thus to some extent "natural). While some might want to argue now that this is due to the "uniform"-character of a vector, I think/wish that it should be possible to achieve this and I really would like to know how, as this is puzzling me for quite some time now...</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>To perhaps clarify the motivation behind my question, I want to program an Exporter-class and let different, more specialized classes derive from it. While the top-level Exporter-class is generally only for cosmetic/structural purpose, a GraphExporter-class is derived, that should again serve as a base-class for even more specialzed export. However, similar to the Animal-example, I would like to be able to define GraphExporter* even on specialized/derived classes (e.g. on SpecialGraphExplorer) but when calling "write( out_file )", it should call the appropriate member function for SpecialGraphExporter instead of GraphExporter::write( out_file).</p>
  
  <p>Maybe this makes my situation and intentions clearer.</p>
  
  <p>Best,</p>
  
  <p>Shadow</p>