Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We first need some helper templates. The first one checks if two types are the same:</p> <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T, typename U&gt; struct is_same { // Default case: T and U are not the same type static const bool value = false; }; template &lt;typename T&gt; struct is_same&lt;T, T&gt; { // Specialization: both template arguments are of the same type static const bool value = true; }; </code></pre> <p>We can now use the boolean <code>is_same&lt;T, U&gt;::value</code> to determine if the types <code>T</code> and <code>U</code> are equivalent.</p> <p>Using <code>is_same</code> we can now write a template which checks if a specific type occurs in a type list:</p> <pre><code>template &lt;typename TList, typename T&gt; struct contains { static const bool value = is_same&lt;typename TList::head, T&gt;::value // Base case || contains&lt;typename TList::tail, T&gt;::value; // Recursion }; template &lt;typename T&gt; struct contains&lt;nulltype, T&gt; { // Termination condition static const bool value = false; }; </code></pre> <p>This is a recursive template, using a specialization on <code>nulltype</code> to terminate the recursion.</p> <p>One final helper template we need is <code>enable_if</code>:</p> <pre><code>template &lt;bool Cond, typename T=void&gt; struct enable_if { // Default case: Cond assumed to be false, no typedef }; template &lt;typename T&gt; struct enable_if&lt;true, T&gt; { // Specialization: Cond is true, so typedef typedef T type; }; </code></pre> <p><code>enable_if&lt;true, T&gt;::type</code> yields <code>T</code>, whereas <code>enable_if&lt;false, T&gt;::type</code> is undefined. We exploit the <a href="http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error" rel="nofollow">SFINAE</a> rule the enable or disable a function depending on its template argument, like so:</p> <pre><code>template &lt;typename TList&gt; class OutputClass { public: // Only enable the function if T is in TList (by SFINAE) template &lt;typename T&gt; typename enable_if&lt;contains&lt;TList, T&gt;::value&gt;::type output(T t) { std::cout &lt;&lt; t &lt;&lt; std::endl; } }; </code></pre> <p>Quite a trip, but if you understand all of this you're well on your way to master template metaprogramming. For an in-depth discussion of template metaprogramming I recommend you pick up a copy of <a href="https://www.pearsonhighered.com/program/Abrahams-C-Template-Metaprogramming-Concepts-Tools-and-Techniques-from-Boost-and-Beyond/PGM93893.html" rel="nofollow">C++ Template Metaprogramming</a> (ISBN-13: 9780321227256).</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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