title: How can I force a compiler error if an un-overridden virtual method is called?
body: <p>This is a rather general question about style and safety when writing a template base class in C++. Bear with me, though, there is a specific question at the end...</p>
  
  <p>I have a template base class which fully implements the required functionality when the type T is a primitive type (int, float, etc.). However,if T is not primitive (for example, if T is a class that needs to have a constructor called with a specific set of arguments) then some of the methods will need to be overridden, and for this reason they are declared virtual in the template. The template also contains a pure virtual method which forces it to be abstract, so in order to be used it <em>must</em> be derived from: a derived class based on a primitive type can use all the methods as provided and override just the pure virtual one, whilst a derived class based on a non-primitive type should override all the virtual methods.</p>
  
  <p>For example:</p>
  
  <pre><code>template &lt;typename T&gt; class myTemplate
  {
  public:
      // Constructor:
      myTemplate&lt;T&gt;();
  
      // Destructor:
      virtual ~myTemplate();
  
      // General function, valid for all classes derived from this:
      void printMe()
      {
          printf("Hello, I'm from the template\r\n");
      }
  
      // Primitive function, must be overridden for non-primitive types:
      virtual T DoSomething(const T&amp; myClass)
      {
          T result = result + static_cast&lt;T&gt;(1);
          return result;
      }
  
      // Primitive function, must be overridden for non-primitive types:
      virtual T DoSomethingElse(const T&amp; myClass)
      {
          T result = result - static_cast&lt;T&gt;(1);
          return result;
      }
  
      // Pure Virtual function, must be overridden in all cases:
      virtual void RefrainFromDoingAnythingAtAll(const T&amp; myClass) = 0
  };
  
  
  class myIntegerClass : public myTemplate&lt;int&gt;
  {
  public:
      virtual void RefrainFromDoingAnythingAtAll(const int&amp; myInteger) {} 
  };
  </code></pre>
  
  <p>Suppose now I want to create a derived class where I expect 'DoSomething' to be called, but can't envisage any circumstances under which 'DoSomethingElse' would be useful. Therefore I'd like to reimplement 'DoSomething', but not bother with 'DoSomethingElse'. Nevertheless, if at some point the derived class's 'DoSomethingElse' method <em>does</em> get called (either because I really meant to call 'DoSomething' but wrote the wrong thing by mistake, or because a circumstance arose which I had earlier failed to envisage), then I want a compiler warning to be issued to remind me that I can't do this unless I reimplement 'DoSomethingElse' first.</p>
  
  <p>For example:</p>
  
  <pre><code>class myWatermelonClass : public myTemplate&lt;Watermelon&gt;
  {
  public:
      virtual Watermelon DoSomething(const Watermelon &amp;myWatermelon)
      {
            // Just return a copy of the original:
            Watermelon anotherWatermelon(myWatermelon);
            return anotherWatermelon;
      }
  
      virtual Watermelon DoSomethingElse(const Watermelon &amp;myWatermelon)
      {
            // This routine shouldn't ever get called: if it does, then 
            // either I've made an error or, if I find that I really need
            // to reimplement it after all, then I'd better buckle down
            // and do it.
            &lt; How do I make the compiler remind me of this? &gt;
      }        
  
      virtual void RefrainFromDoingAnythingAtAll(const Watermelon&amp; myWatermelon) {}   
  };
  </code></pre>
  
  <p>Obviously I know about the standard #error and #warning compiler directives, but if I use one of these then the error (or warning) is flagged every time I compile. What I want is to make sure that an error is given at compile time if I negligently call</p>
  
  <pre><code>DoSomethingElse(aSpecificWatermelon);
  </code></pre>
  
  <p>from somewhere in my code, <em>but not otherwise</em>. Is there a way to achieve this? Or is this just a fundamentally bad design in the first place?</p>