title: C++ ::std::enable_if and virtual
body: <p>I've had a scan for answers but ... they seem to be silly questions.</p>
  
  <p>I am perfectly happy with and fully understand (I can't stress that enough) with why it makes no sense to have a template virtual member of a class.</p>
  
  <p>Consider the abstract base class <code>List</code> (With... <code>LinkedList</code> and <code>ArrayList</code> deriving from it)</p>
  
  <p>If the type the list stores has a concept of identity (not a string or int, something without any sensible "==", I wont bring POD up here) you would want a method<br>
  <code>virtual bool contains(T&amp; what) const;</code> and<br>
  <code>virtual int index(T&amp; what) const;</code><br>
  however if it is a type without identity, like strings or numbers you would want:<br>
  <code>virtual int countOccurrences(T&amp; what) const;</code> and<br>
  <code>virtual int find(T&amp; what, int occurrence=0) const;</code></p>
  
  <p>Say. </p>
  
  <p>This cannot be done using <code>::std::enable_if</code> so you must do something like:</p>
  
  <pre><code>template&lt;class T, bool HAS_IDENTITY&gt; class List;
  template&lt;class T&gt; class List&lt;false&gt; {
      virtual int countOccurrences(T&amp; what) const=0;
      virtual int find(T&amp; what, int occurrence=0) const=0;
      /*other stuff*/
  };
  
  template&lt;class T&gt; class List&lt;true&gt; {
      virtual bool contains(T&amp; what) const =0;
      virtual int index(T&amp; what) const =0;
      /*other stuff*/
  };
  </code></pre>
  
  <p>This isn't that bad, but there is a lot of code duplication, and I only get wet (against DRY) when I have to.</p>
  
  <p>If I hide the common code in a base class it is a bit nicer.</p>
  
  <p>My question involves scaling with this approach, here we have one bool, giving 2 specialisations, suppose I have n bools then there are 2^n specialisations, I can't see a case where I'd need more than 4, but that is still 16 classes involved! 8 for 3, it's not very nice. </p>
  
  <p>Suppose I have an enum and a bool, then I have 2*enum count specialisations.</p>
  
  <p>It grows far to quickly. </p>
  
  <p>Previously we've used macros to define classes and it'd use the <code>##</code> operator in the class name to essentially mangle it as a template would. I must say though I quite like <code>enable_if</code> and friends now though...</p>
  
  <p>Is there a pattern someone can show me that'd solve this? </p>