title: Conditionally remove methods in template class
body: <p><strong>Background</strong>: The usage of STL makes builds slow due to template code bloat: quite often the same methods are independently instantiated in many different translation units, being compiled and optimized many times. One way to avoid this object code duplication for template classes is to use <em>explicit template instantiation</em> and <em>extern template declaration</em>, but STL implementations do not support them. I'm trying to implement an equivalent of <code>std::vector&lt;T&gt;</code> which would support explicit instantiations.</p>
  
  <hr>
  
  <p><strong>Question</strong>: I have a template class <code>vector&lt;T&gt;</code>, and I want some of its methods to be removed if the template argument <code>T</code> does not satisfy some condition.
  To make things worse, here are additional requirements:</p>
  
  <ol>
  <li>It must be possible to explicitly instantiate <code>vector&lt;T&gt;</code>, regardless if whether <code>T</code> satisfies the condition or not.</li>
  <li>When someone calls a conditionally removed method, a compilation error should be emitted.</li>
  <li>I have several such conditions, and a set of methods dependent on each of them.</li>
  </ol>
  
  <hr>
  
  <p><strong>Discussion</strong>: For example, the method <code>vector&lt;T&gt;::push_back(const T&amp;)</code> cannot work when <code>T</code> is not copy-constructible.</p>
  
  <p>If I leave this method implementation intact, then compiler will produce error if I explicitly instantiate e.g. <code>vector&lt;unique_ptr&lt;int&gt;&gt;</code>, because it won't be able to copy <code>unique_ptr&lt;int&gt;</code>. This contradicts requirement 1, and makes the desired code bloat optimization impossible.</p>
  
  <p>I can make two different implementations of <code>vector&lt;T&gt;::push_back(const T&amp;)</code>, one for copy-constructible types and one for the others. This can be done using SFINAE overloading or with a helper template class. The implementation for non-copy-constructible case can simply throw exception or call terminate. But then calling the method <code>push_back</code> for <code>vector&lt;unique_ptr&lt;int&gt;&gt;</code> will only crash during runtime. It won't generate compile error, as said in requirement 2.</p>
  
  <p>Finally, there are several conditions, each of them rules out some methods. The type <code>T</code> can lack the following properties in various combinations: copy-constructible, copy-assignable, default-constructible, move-constructible. The solution from <a href="https://stackoverflow.com/questions/26633239/c-templates-conditionally-enabled-member-function">the related question</a> seems to work for one condition only, which does not satisfy requirement 3.</p>
  
  <p>The only idea I have left is to use some sort of partial specialization of the whole <code>vector&lt;T&gt;</code> combined with preprocessor hacks. But this would need something like 16 separate specializations, which sounds terrible.</p>
  
  <p><strong>P.S.</strong> It becomes pretty obvious to me that STL design is inherently dependent on the mechanics of implicit instantiation, which makes it very hard to reduce the code bloat caused by them.</p>