title: Conditional code compilation with C++ templates
body: <p>I have a template class and I would like to conditionally compile code depending on a <code>static constexpr bool</code> member of the template parameter <code>T</code>:</p>
  
  <pre><code>template &lt;typename T&gt;
  class A : public T {
    explicit A() {
       ...
       if(T::boolmember) {
          T::intmember1++;
       } else {
          T::intmember2++
       }
    }
  }
  </code></pre>
  
  <p>This will not compile: only types <code>T</code> which have member <code>boolmember</code> as <code>true</code> contain a member variable <code>intmember1</code>; only types <code>T</code> which have <code>boolmember</code> as <code>false</code> contain a member variable <code>intmember2</code>.</p>
  
  <p>How could I do this? Of course one option is to write template specializations for each function where this situation occurs. There are two issues with this approach:</p>
  
  <ul>
  <li>Variable <code>T::boolmember</code> is not one of the parameters of the class template.</li>
  <li>This situation occurs very often throughout my code, always in small pieces of code, and with different conditions. Doing so would defeat the purpose of template metaprogramming.</li>
  </ul>
  
  <p>Any help is very appreciated.</p>