Note that there are some explanatory texts on larger screens.

plurals
  1. POtemplate rebinding vs. late template binding or how to call piggy backing a template in a type
    primarykey
    data
    text
    <p>Often conceptually a <em>template</em> must be passed as a <em>type argument</em> and the compiler complains because this is not legal c++ - at least until and including c++11 (<strong>Update II:</strong> except see last example of non specialized template definition)</p> <p>These concepts are often used and should have a name. See the code examples below if it is not clear what I mean.</p> <p>My first thought was that this may also be called <em>passing an incomplete type</em> but this is not correct. Another user also states that he has no word for it and freely names it <a href="https://stackoverflow.com/q/15029611/2712726">late binding of template arguments</a>. I think his terminology visualizes the concept well.</p> <p>My question is how do you call the idiom behind this or the involved templates properly?</p> <p><strong>UPDATE</strong> Kerrek suggests naming the idiom <strong>template rebinding</strong>. This name renders only a few google results. However I think it is a very good name too because the standard allocators call their related wrapped internal classes <strong>rebind</strong>.</p> <p>In the following example you could configure if your database uses <code>map</code> or <code>hashmap</code> internally:</p> <pre><code>#include &lt;map&gt; struct map_wrapper { template&lt;typename K, typename V&gt; using type = std::map&lt;K,V&gt;; }; template&lt;typename storage&gt; struct my_database { typename storage::template type&lt;int, int&gt; x; typename storage::template type&lt;int, double&gt; y; }; main () { my_database&lt;map_wrapper&gt; db; db.x[0] = 1; db.y[0] = 2.0; } </code></pre> <p>Or similarly</p> <pre><code>#include &lt;map&gt; #include &lt;boost/mpl/apply.hpp&gt; template &lt;typename storage&gt; struct my_database { typename boost::mpl::apply&lt;storage, int&gt;::type x; typename boost::mpl::apply&lt;storage, double&gt;::type y; }; int main () { my_database&lt; std::map&lt;int, boost::mpl::_1&gt; &gt; db; db.x[0] = 1; db.y[0] = 2.0; } </code></pre> <p><strong>Update II:</strong> To my embarrassment I did not know the following solution which is just passing a template as argument to a template instantiation. In this special case it is legal to pass a non-type.</p> <pre><code>#include &lt;map&gt; #include &lt;unordered_map&gt; template&lt;template&lt;typename...&gt; class Storage&gt; struct my_database { Storage &lt;long,char&gt; x; Storage &lt;long,double&gt; y; }; int main () { my_database&lt; std::map &gt; db1; db1.x[0] = '1'; db1.y[0] = 2.2; my_database&lt; std::unordered_map &gt; db2; db2.x[0] = '1'; db2.y[0] = 2.2; } </code></pre> <p>Examples of other ways to <strong>rebind</strong> or <strong>late bind</strong> template arguments are of course welcomed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload