Here’s an interesting problem involving C++ templates. Say you have a class that is parameterized on a term — we'll use std::bitset<N> as a running example — and you’d like to wrap this class in another class that hides the value of N from its clients.

<p>Of course, it isn&#8217;t possible to instantiate a template based on a value that isn&#8217;t a compile-time constant, but it may be desirable to choose from one of several (statically-defined) values at run-time.  In the <tt>std::bitset</tt> case, we might define &#8220;small,&#8221; &#8220;medium,&#8221; and &#8220;large&#8221; sizes for a representation class, and have the constructor for wrapper class choose the representation that is large enough to hold the set of bits it is created with.  We&#8217;d like wrap <tt>bitset</tt> in such a way so that its size is not an explicit part of the type of the wrapper class (and thus clients can write code that handles wrapped-<tt>bitset</tt> objects of varying sizes without parameterizing their code).  This is a problem, because the size of <tt>bitset</tt> is part of its type.</p>


In general, this problem manifests whenever a wrapper class can make a run-time decision about which of some static set of template parameter values for its (hidden) representation class is most appropriate; or, still more generally, when it makes sense to choose a different type of representation based on a run-time value. Note that this problem isn't confined to classes parameterized on terms; a similar problem could also appear with classes parameterized on types, like STL containers. However, it is less immediately clear to me what a solution for classes parameterized on types would look like in the general case, since the type parameter is more likely to be exposed in the container's interface. (There are special cases, like choosing different types of containers as an internal representation depending on the characteristics of different collections of homogeneous data, that might make sense.)

<p>There are a few approaches to wrapping a class in C++, but none of them are particularly suitable for solving this problem.  Here&#8217;s why:</p>

1. Public inheritance does not shield clients from the template value in the representation class, because it must be a template parameter to the wrapper class. However, public inheritance frees the wrapper class implementor from implementing (potentially) many proxy methods that simply call out to to the representation class.
2. Explicit delegation requires the wrapper class implementor to write proxy methods for each operation of interest from the representation class. However, the template parameter to the representation class is still part of its type. Therefore, there must be several fields in the wrapper class that hold pointers to instances of different instantiations of the representation class template (e.g. std::bitset<32>, std::bitset<128>, and std::bitset<2048>). In addition, each proxy method must include a conditional expression that determines which of these objects is the actual representation and a pointer dereference. (Having the wrapper class include three std::bitset values, rather than pointers, would defeat the purpose of having “small,” “medium,” and “large” representations.)
3. Private inheritance combines the problems of the first two approaches with the advantages of neither.
<p>A general solution to this problem at the type-system level might involve constrained template parameters and union types.  So you could say, e.g., <tt>std::bitset&lt;N in {32,128,2048}&gt; b = std::bitset&lt;32&gt;();</tt>  This solves our problem by encoding a range of possible sizes in the type of a <tt>bitset</tt>-valued variable rather than encoding a particular size.  (My gut feeling &#8212;  although I don&#8217;t have the turnstiles to prove it &#8212;  is that this feature might be difficult or impossible to include in a sound, statically-checked type system, let alone to implement in a production compiler.)  Sadly, extending the language is rarely a viable option for most software projects.</p>

<p>There are, of course, ways to solve the problem of needing a set of bits large enough to hold <em>k</em> bits, where <em>k</em> isn't known until runtime: use <tt>std::bit_vector</tt> or use <tt>std::vector&lt;std::bitset&lt;32&gt; &gt;</tt>, to give two simple examples.  More generally, it would also be possible to use <tt>virtual</tt> functions in a base <tt>representation</tt> class that is extended by a templated class:  then <tt>derived&lt;32&gt;</tt> and <tt>derived&lt;128&gt;</tt> could both be pointed to by a <tt>representation*</tt>.  But I wonder, since I&#8217;m continually amazed by feats of template metaprogramming, what clean and efficient solutions to the general problem are possible?</p>​


type systemsc++ • You may reply to this post on Twitter or