title: Dynamic compile time mixins
body: <p>I'm trying to come up with a way to define dynamic mixins at compile time. I currently have a very hacky solution that only partially does what I want, but I'm not sure how to improve it.</p>
  
  <p>I'm aware of some more C++ like solutions using typelist's, but they require all of the types to be defined statically which I'm trying to avoid.  </p>
  
  <p>This is mostly just a thought exercise to learn C++ better, and I'm sure that my current implementation is not very good C++.  Any suggestions for improvements or different ideas to try would be welcome.</p>
  
  <p>The main problems with my current implementation are:</p>
  
  <ul>
  <li>Each mixin class is only aware of itself and classes lower then itself in the hierarchy. I'd like each mixin class to be able to return a new mixin with a different underlying type.  In the example code below I'd like <code>PrintOnce</code> to be able to have a method which returns a <code>PrintTwice&lt;PrintOnce&lt;Printer&lt;int &gt; &gt; &gt;</code> object.</li>
  <li>Any usage of this idea would need to have an order of header inclusion.  All the boiler plate code in the beginning would need to be in one header, then all of the mixin classes would need to be defined, and finally the <code>make_mixed</code> function could be defined.  Currently any mixin defined after the <code>make_mixed</code> function is ignored.  </li>
  <li>The macro's and general hackyness of the implementation.</li>
  </ul>
  
  <p>I apologize for the length, but this is the most simplified example I could come up with.</p>
  
  <p>Thanks for any help.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  template&lt;class Underlying&gt;
  struct Printer
  {
      Printer(const Underlying &amp;val) : val_(val) {}
  
      Underlying get_val() { return val_; }
  
    private:
      Underlying val_;
  };
  
  #define CURRENT_NUMBER_MIXED_IN_CLASSES() \
      MixinCount&lt;0, __LINE__&gt;::value
  
  #define INCREMENT_MIXIN_CLASS_COUNTER() \
      template&lt;int id&gt;                                               \
      struct MixinClassCounter&lt; CURRENT_NUMBER_MIXED_IN_CLASSES(), id&gt;    \
      {                                                                   \
          static const bool is_defined = true;                            \
      }
  
  template&lt; bool b, typename i, typename j &gt;
  struct select_value;
  
  template&lt;class i, class j&gt;
  struct select_value&lt;true, i, j&gt;
  {
      static const int value = i::value;
  };
  
  template&lt;class i, class j&gt;
  struct select_value&lt;false, i, j&gt;
  {
      static const int value = j::value;
  };
  
  template&lt;int i&gt;
  struct IntToVal
  {
      static const int value = i;
  };
  
  namespace
  {
      template&lt;int count, int id&gt;
      struct MixinClassCounter
      {
          static const bool is_defined = false;
      };
  
      template&lt;int count, int id&gt;
      struct MixinCount
      {
          static const int value = select_value&lt;MixinClassCounter&lt;count, id&gt;::is_defined,
                                                MixinCount&lt;count + 1, id&gt;,
                                                IntToVal&lt;count&gt; &gt;::value;
      };
  
      template&lt;class Underlying, int i&gt;
      struct MixinBuilder {};
  
      template&lt;class Underlying&gt;
      struct MixinBuilder&lt;Underlying, 0&gt;
      {
          typedef Printer&lt;Underlying&gt; type;
      };
      INCREMENT_MIXIN_CLASS_COUNTER();
  }
  
  #define DECLARE_MIXIN_BEGIN(name) \
      template&lt;class Base&gt; \
      struct name : Base   \
      {                    \
          template&lt;class Underlying&gt;                  \
          name(const Underlying &amp;val) : Base(val) {}
  
  #define DECLARE_MIXIN_END(name)                 \
      };                                          \
      namespace                                   \
      {                                               \
          template&lt;class Underlying&gt;                                         \
          struct MixinBuilder&lt;Underlying, CURRENT_NUMBER_MIXED_IN_CLASSES()&gt; \
          {                                                                   \
              typedef name&lt; typename MixinBuilder&lt;Underlying, CURRENT_NUMBER_MIXED_IN_CLASSES() - 1&gt;::type &gt; type; \
          };                                                                  \
          INCREMENT_MIXIN_CLASS_COUNTER();                                    \
      }                                                                       \
  
  
  DECLARE_MIXIN_BEGIN(PrintOnce)
  void print_once()
  {
      cout &lt;&lt; Base::get_val() &lt;&lt; endl;
  }
  DECLARE_MIXIN_END(PrintOnce)
  
  DECLARE_MIXIN_BEGIN(PrintTwice)
  void print_twice()
  {
      cout &lt;&lt; Base::get_val() &lt;&lt; endl;
      cout &lt;&lt; Base::get_val() &lt;&lt; endl;
  }
  DECLARE_MIXIN_END(PrintTwice)
  
  template&lt;class T&gt;
  typename MixinBuilder&lt;T, CURRENT_NUMBER_MIXED_IN_CLASSES() - 1&gt;::type make_mixed(const T &amp;val)
  {
      return typename MixinBuilder&lt;T, CURRENT_NUMBER_MIXED_IN_CLASSES() - 1&gt;::type(val);
  }
  
  int main()
  {
      string test("this is a test");
      auto printable_string = make_mixed(test);
  
      printable_string.print_once();
      printable_string.print_twice();
  }
  </code></pre>