title: C++ interface without virtual functions
body: <p>I was wondering how we can declare an interface in C++ without using virtual functions. After some internet searching I put together this solution:</p>
  
  <pre><code>#include &lt;type_traits&gt;
  
  using namespace std;
  
  // Definition of a type trait to check if a class defines a member function "bool foo(bool)"
  template&lt;typename T, typename = void&gt;
  struct has_foo : false_type { };
  
  template&lt;typename T&gt;
  struct has_foo&lt;T, typename enable_if&lt;is_same&lt;bool, decltype(std::declval&lt;T&gt;().foo(bool()))&gt;::value, void&gt;::type&gt; : true_type { };
  
  // Definition of a type trait to check if a class defines a member function "void bar()"    
  template&lt;typename T, typename = void&gt;
  struct has_bar : false_type { };
  
  template&lt;typename T&gt;
  struct has_bar&lt;T, typename enable_if&lt;is_same&lt;void, decltype(std::declval&lt;T&gt;().bar())&gt;::value, void&gt;::type&gt; : true_type { };
  
  // Class defining the interface
  template &lt;typename T&gt;
  class Interface{
  public:
    Interface(){
      static_assert(has_foo&lt;T&gt;::value == true, "member function foo not implemented");
      static_assert(has_bar&lt;T&gt;::value == true, "member function bar not implemented");
    }
  };
  
  // Interface implementation
  class Implementation:Interface&lt;Implementation&gt;{
  public:
    // If the following member functions are not declared a compilation error is returned by the compiler
    bool foo(bool in){return !in;}
    void bar(){}
  };
  
  int main(){}
  </code></pre>
  
  <p>I'm planning to use this design strategy in a project where I will use static polymorphism only.
  The C++ standard I will use in the project is C++11.</p>
  
  <p>What do you think are the pros and cons of this approach?</p>
  
  <p>What improvements can be made on the code I proposed?</p>
  
  <p><strong>EDIT 1:</strong>
  I just realised that inheriting from Interface is not needed. This code could also be used:</p>
  
  <pre><code>class Implementation{
    Interface&lt;Implementation&gt; unused;
  public:
    bool foo(bool in){return !in;}
    void bar(){}
  };
  </code></pre>
  
  <p><strong>EDIT 2-3:</strong>
  One major difference between the static_assert solution (with or without CRTP) and the standard CRTP is that the CRTP does not guarantee that the derived class implements all the interface members. E.g., the following code compiles correctly:</p>
  
  <pre><code>#include &lt;type_traits&gt;
  using namespace std;
  
  template&lt; typename T&gt;
  class Interface{
  public:
    bool foo(bool in){
      return static_cast&lt;T*&gt;(this)-&gt;foo(in);
    }
    void bar(){
      static_cast&lt;T*&gt;(this)-&gt;bar();
    }
  };
  
  class Implementation: public Interface&lt;Implementation&gt;{
  public:
  //    bool foo(bool in){return !in;}
  //    void bar(){}
  };
  
  int main(){}
  </code></pre>
  
  <p>An error about a missing member function will be returned by the compiler only when the functions <em>foo</em> or <em>bar</em> will be required.</p>
  
  <p>The way I see it, the static_assert solution feels more like an interface declaration than CRTP alone.</p>