title: C++17 Pass reference to interface of templated class into sibling templated class constructor
body: <p>Consider the following example:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename Consumer&gt;
  class ClassA
  {
  public:
      template&lt;class... Args&gt;
      explicit ClassA(Args &amp;&amp;... args) :
          consumer_(std::forward&lt;Args&gt;(args)...)
      {
  
      }
  
      void consume()
      {
          consumer_.consume();
      }
  
  private:
      Consumer consumer_;
  };
  
  template &lt;typename Consumer&gt;
  class ClassB
  {
  public:
      template&lt;class... Args&gt;
      explicit ClassB(Args &amp;&amp;... args) :
          consumer_(std::forward&lt;Args&gt;(args)...)
      {
  
      }
  
      void consume()
      {
          consumer_.consume();
      }
  
  private:
      Consumer consumer_;
  };
  
  class ClassC
  {
  public:
      explicit ClassC(int val) :
          val_(val)
      {
  
      }
  
      void consume()
      {
          std::cout &lt;&lt; &quot;ok &quot; &lt;&lt; val_ &lt;&lt; std::endl;
  
      }
  
  private:
      int val_;
  };
  
  
  void usage()
  {
      ClassA&lt;ClassB&lt;ClassC&gt;&gt; composed_object(3);
      composed_object.consume();
  }
  </code></pre>
  <p>It's a kind of templated bridge (or proxy? or strategy?) pattern, that I can easily compose and change implementation in compile time.</p>
  <p>I try to avoid using of dynamic polymorphism due to performance reasons.</p>
  <p><strong>So, the question</strong>: How to allow ClassB to call some of ClassA methods?</p>
  <p>The first thing that comes to my mind is to pass a reference of ClassA to ClassB. But there is a templated class chain and I don't want to change class chain usage.</p>
  <p>I can rewrite ClassB as follows:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename Interface, typename Consumer&gt;
  class ClassB
  {
  public:
      template&lt;class... Args&gt;
      explicit ClassB(Interface &amp;interface, Args &amp;&amp;... args) :
          consumer_(std::forward&lt;Args&gt;(args)...),
          interface_(interface)
      {
  
      }
  
      void consume()
      {
          consumer_.consume();
      }
  
  private:
      Consumer consumer_;
      Interface &amp;interface_;
  };
  </code></pre>
  <p>So, it now takes an additional templated param of parent class Interface and a reference to parent in constructor.</p>
  <p>But I have no idea how to specify templated param Interface in ClassA without change the chain usage.</p>
  <p>To my mind it's a kind of template param infinity loop.</p>