title: Understanding C++ Traits and Making Them Efficient
body: <p>I have come across the interesting and powerful concept of &quot;traits&quot; recently and am attempting to understand/implement them in C++.  From what I understand, traits provide a way both extend/adapt the functionality of exiting code and define an &quot;interface&quot; for a class without using traditional inheritance (and all the overhead/problems that come with it).  I also see that this concept seems to be closely related to the CRTP design pattern in C++.</p>
  <p>As an example, my normal thought process for writing an interface in C++ would be to define a class with pure virtual methods.  I can then create a subclass of this and pass a pointer to all my generic code. I am discovering that this has some problems however:</p>
  <ol>
  <li>Classes that need to inherit from multiple interfaces require the use of multiple inheritance which can become very complex and introduce the &quot;diamond pattern&quot; problem.</li>
  <li>A strict &quot;is a&quot; relationship is formed which is not always the intent.  For example, if I am describing the interface to a Light, a Simulated Light is NOT really a Light, it merely has the &quot;traits&quot;/acts like a Light.  What I mean is, the generic Light interface doesn't really have commonalities that an implementation needs to inherit, it merely defines how an implementation should behave.</li>
  <li>Virtual methods and inheritance allow full dynamic polymorphism which incurs unnecessary overhead. In most of my code, I would only ever be using a single implementation of an interface at a time and thus I don't need to dynamically choose the correct implementation, I just need to have the &quot;users&quot; of the interface be generic enough to all for different implementations.</li>
  </ol>
  <p>Here is an example of a simple, traditional interface for a Light:</p>
  <pre><code>class Light {
  public:
      virtual void on() = 0;
      virtual void off() = 0;
  };
  
  class MyLight : public Light {
  public:
      void on() override;
      void off() override;
  };
  
  void lightController(Light&amp; l) {
      l.on();
      l.off();
  }
  </code></pre>
  <p>And (based upon the article here: <a href="https://chrisbranch.co.uk/2015/02/make-your-c-interfaces-trait-forward/" rel="noreferrer">https://chrisbranch.co.uk/2015/02/make-your-c-interfaces-trait-forward/</a>) here is what I think is a &quot;traits-based&quot; implementation of the same concept:</p>
  <pre><code>template&lt;typename T&gt;
  class Light {
  public:
      Light(T&amp; self) : _self(self) {}
  
      void on() { _self.on(); }
      void off() { _self.off(); }
  
  private:
      T&amp; _self;
  };
  
  class MyLight {
  public:
      void on();
      void off();
  };
  
  class OddLight {
  public:
      void set(bool state);
  };
  
  template&lt;&gt;
  class Light&lt;OddLight&gt; {
  public:
      Light(OddLight&amp; self) : _self(self) {}
      
      void on() { _self.set(true); }
      void off() { _self.set(false); }
      
  private:
      OddLight&amp; _self;
  };
  
  template&lt;typename T&gt;
  void lightUser1(T&amp; l) {
      Light&lt;T&gt; light(l);
  
      light.on();
      light.off();
  }
  
  template&lt;typename T&gt;
  void lightUser2(Light&lt;T&gt;&amp; l) {
      light.on();
      light.off();
  }
  </code></pre>
  <p>I have a few questions about this:</p>
  <ol>
  <li>Because, to use traits like this, you (temporarily) create a new Light instance, is there memory overhead associated with this?</li>
  <li>Is there a more effective method to document that a particular class &quot;implements&quot; a given trait?</li>
  <li>The article mentions two methods of defining a &quot;user&quot; for an interface. I have shown both above.  lightUser2 seems to be the most well-document (it explicitly states that the function requires some implementation of the Light trait), however it requires that the implementations be explicitly casted into a Light outside of the function. Is there method to both document the intent of the user and all an implementation to be passed in directly?</li>
  </ol>
  <p>Thank You!</p>