Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a template parameter in another template parameter declared before
    primarykey
    data
    text
    <p>a template parameter can be used in another template parameter that follows it this way :</p> <pre><code>template&lt;typename T, T N&gt; struct s { }; </code></pre> <p>But is it possible to reference "T" if it is declared after "N" ?</p> <p>This does not work :</p> <pre><code>template&lt;T N, typename T&gt; struct s { }; </code></pre> <p>Can we help the compiler by pre-declaring "T" or doing anything else ?</p> <p>Thanks by advance.</p> <p>EDIT : as the first two replies were asking "why are you willing to do that ?" I'll explain the goal :</p> <p>I would like to make the compiler infer the type "T" in order to make the use of templated classes easier.</p> <p>For example :</p> <pre><code>template&lt;typename T, T A, T B&gt; struct sum { static T const value = A + B; }; </code></pre> <p>This template can be used this way :</p> <pre><code>sum&lt;int, 1, 2&gt;::value </code></pre> <p>But it would be better if it could be used this way :</p> <pre><code>sum&lt;1, 2&gt;::value </code></pre> <p>Technically it's should be possible because the compiler knows the types of "1" and "2" : "int", and in fact it uses these informations to find the best overload for a function. So by declaring the template this way :</p> <pre><code>template&lt;T A, T B, typename T&gt; struct sum { static T const value = A + B; }; </code></pre> <p>the compiler could use its capability to infer the last parameter from the informations provided by the first and the second one, and then find the best template to instantiate.</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.
 

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