title: Template (de)activated member variables
body: <p>I am looking for a convenient to create a <code>C++</code> class where some member variables are only present if a template flag is set. As a simple example, let's assume I want to toggle an averageSum in an performance sensitive calculation, i.e.</p>
  <pre><code>struct Foo {
      // Some data and functions..
      
      void operator+=(const Foo&amp; _other) {}
  };
  
  
  
  template&lt;bool sumAverages&gt;
  class Calculator {
  public:
      // Some member variables...
      
      // Those should only be present if sumAverages is true
      int count = 0;
      Foo resultSum;
      
      void calculate(/* some arguments */) {
          // Calculation of result...
          Foo result;
          
          // This should only be calculated if sumAverages is true
          ++count;
          resultSum += result;
          
          // Possibly some post processing...
      }
  };
  </code></pre>
  <p>One way would be using preprocessor defines, but those are rather inconvenient especially if I need both versions in the same binary. So I am looking for an alternative using templates and <code>if constexpr</code> and something like the following <code>Conditional</code> class:</p>
  <pre><code>template&lt;bool active, class T&gt;
  struct Conditional;
  
  template&lt;class T&gt;
  struct Conditional&lt;true, T&gt; : public T {};
  
  template&lt;class T&gt;
  struct Conditional&lt;false, T&gt; {};
  </code></pre>
  <p>My first shot was this:</p>
  <pre><code>template&lt;bool sumAverages&gt;
  class Calculator {
  public:
      int count = 0;
      Conditional&lt;sumAverages, Foo&gt; resultSum;
      
      void calculate(/* some arguments */) {
          Foo result;
          
          if constexpr(sumAverages) {
              ++count;
              resultSum += result;
          }
      }
  };
  </code></pre>
  <p>The <code>if constexpr</code> should incur no run time cost and as it is dependent on a template variable should allow non-compiling code in the <code>false</code> case (e.g. in this example <code>Conditional&lt;false, Foo&gt;</code> does not define a <code>+=</code> operator, still it compiles). So this part is more or less perfect. However the variables <code>count</code> and <code>resultSum</code> are still somewhat present. In particular, as one can not derive from a fundamental type, the <code>Conditional</code> class does not allow to toggle the <code>int</code> dependent on the template. Furthermore every <code>Conditional&lt;false, T&gt;</code> variable still occupies one byte possibly bloating small classes. This could be solvable by the new <code>[[no_unique_address]]</code> attribute, however my current compiler chooses to ignore it in all my tests, still using at leas one byte per variable.</p>
  <p>To improve things I tried inheriting the variables like this</p>
  <pre><code>struct OptionalMembers {
      int count;
      Foo resultSum;
  };
  
  template&lt;bool sumAverages&gt;
  class Calculator : public Conditional&lt;sumAverages, OptionalMembers&gt; {
  public:
      void calculate(/* some arguments */) {
          Foo result;
          
          if constexpr(sumAverages) {
              ++OptionalMembers::count;
              OptionalMembers::resultSum += result;
          }
      }
  };
  </code></pre>
  <p>This should come at no space cost as inheriting from am empty class should do literally nothing, right? A possible disadvantage is that one cannot freely set the order of the variables (the inherited variables always come first).</p>
  <p>My questions are:</p>
  <p>Do you see any problems using the approaches described above?</p>
  <p>Are there better options to de(activate) variables like this?</p>