title: Use C++20 Concepts to force a class to implement a set of methods
body: <p>I am wondering if there is a way in C++ (especially with C++20) to write some kind of interface for classes/structs.</p>
  
  <p>For example in Java interface is a completely "abstract class" that is used to group related methods with empty bodies:</p>
  
  <pre class="lang-java prettyprint-override"><code>interface Animal
  {
    public void animalSound();
    public void run();
  }
  </code></pre>
  
  <p>In C++ you could use a pure virtual method declarations to achieve the same behavior.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Animal
  {
  public:
    virtual void animalSound() = 0;
    virtual void run() = 0;
  };
  </code></pre>
  
  <p>But with virtual methods you have runtime costs, and I am not interested in inheritance.
  So this runtime cost should not be necessary. I just want compile time checks for my "Animal" classes/structs.</p>
  
  <p>With C++20's Concepts I am sure that it is achievable to build a construct
  that you can apply to a class to guarantee that a certain set of methods is provided.</p>
  
  <p>What I was trying to do looked a bit like this.</p>
  
  <pre><code>template&lt;typename Animal_&gt; concept Animal =
  requires()
  {
    (Animal_{}); // default constructable
  
    (Animal_{}.animalSound());
    (Animal_{}.run());
  };
  </code></pre>
  
  <p>But I am not sure that this is very c++ish to do.</p>
  
  <p>(By the way is there a way to require the return type of method to be of a specific type?)</p>
  
  <p>And I am not sure how to attach this to a class/struct.</p>
  
  <p>My first thought was to use a <code>static_assert</code> inside the class/struct:</p>
  
  <pre><code>class Cow
  {
  private: // compile time type checking
    static_assert(std::is_matching_concept&lt;Animal, Cow&gt;);
  
  public:
    void animalSound() const noexcept {}
    void run() const noexcept {}
  };
  </code></pre>
  
  <p>Where <code>std::is_matching_concept</code> is a placeholder for a constraint that I can not find.</p>
  
  <p>I am looking for best practice feedback and suggestions to solve my problem.</p>
  
  <p><strong>EDIT - Add use case</strong></p>
  
  <pre><code>// given the following code
  
  template&lt;typename Vector_, typename Float_=float&gt; concept Vector =
  requires()
  {
    (Vector_{}); // default constructable
  
    (Vector_{}.X())-&gt;Float_;
    (Vector_{}.Y())-&gt;Float_;
  };
  
  [[nodiscard]] constexpr auto Pow2(const auto x) noexcept
  {
    return x * x;
  }
  
  [[nodiscard]] constexpr auto LengthPow2(Vector auto vec) noexcept // the use of Vector
  {
    return Pow2(vec.X()) + Pow2(vec.Y());
  }
  
  // Now I want to implement a Vector
  // and I want compile time checking, that I have no missed any properties
  
  struct VectorImpl1
  {
  // EDITED: as @QuentinUK mentioned the static_assert should be in a public scope
  //         "If in the private part of a class the concepts
  //         can pass for private members which isn't what you'd want."
  public:
    // EDITED: as @DavisHerring mentioned this is the way to go
    static_assert(Vector&lt;VectorImpl1&gt;);
  
  public:
    constexpr VectorImpl1() noexcept = default;
  
    constexpr VectorImpl1(float x, float y) noexcept : x_(x), y_(y) {}
  
  private:
    float x_{};
    float y_{};
  
  public:
    [[nodiscard]] constexpr float X() const noexcept
    { return x_; }
  
    [[nodiscard]] constexpr float Y() const noexcept
    { return y_; }
  };
  
  struct VectorImpl2
  {
  public:
    static_assert(Vector&lt;VectorImpl2&gt;);
  
  public:
    constexpr VectorImpl2() noexcept = default;
  
    constexpr VectorImpl2(float rad, float length) noexcept : rad_(rad), length_(length) {}
  
  private:
    float rad_{};
    float length_{};
  
  public:
    [[nodiscard]] constexpr float X() const noexcept
    { return CalcX(rad_, length_); } 
  
    [[nodiscard]] constexpr float Y() const noexcept
    { return CalcY(rad_, length_); }
  };
  </code></pre>