Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @jogojapan pointed out, the problem is that the compiler cannot order these two functions, i.e. there is not one that is more specialized than the other. As explained in §14.5.6.2, when a call to an overloaded function template is ambiguous, the compiler uses a partial ordering between the various overloads to select the most specialized one.</p> <p>To order the overloads, the compiler transforms each one of them and performs template argument deduction to see if one is more specialized than another one (there is a short explanation at the end of <a href="https://stackoverflow.com/a/13966381/20984">this answer</a>). In your case, the two overloads are equivalent (or not comparable): <code>template&lt;int&gt; void template_const(int &amp;,int &amp;)</code> is not more specialized than <code>template&lt;bool&gt; void template_const(int &amp;, int &amp;)</code>, and vice-versa.</p> <p>Therefore, the compiler cannot select one over the other, hence generating an <code>ambiguous call</code> error.</p> <hr> <p>If you are ok with explicitely specifying the type of the parameter you want to pass, you can use partial template specialization as follow:</p> <pre><code>template&lt;typename T, T param&gt; struct template_const_impl; template &lt;int module&gt; struct template_const_impl&lt;int, module&gt; { static void apply(int &amp;a, int &amp;b) { a = a &amp; module; b = b % module; } }; template&lt;bool x&gt; struct template_const_impl&lt;bool, x&gt; { static void apply(int &amp;a, int &amp;b) { const int w = x ? 123 : 512; a = a &amp; w; b = b % w; } }; template &lt;typename T, T param&gt; void template_const(int &amp;a, int &amp;b) { return template_const_impl&lt;T, param&gt;::apply(a, b); } int main() { int i = 512, j = 256; template_const&lt;int, 123&gt;(i, j); template_const&lt;bool, true&gt;(i, j); } </code></pre> <p>This is not ideal, but it don't think there is a cleaner solution unless you can use C++11 and are willing to rely on some macros, in which case you can simplify the calling code a bit (idea taken from @Nawaz in <a href="https://stackoverflow.com/a/9045644/20984">this answer</a>):</p> <pre><code>#define TEMPLATE_CONST(x) template_const&lt;decltype(x), x&gt; int main() { int i = 512, j = 256; TEMPLATE_CONST(123)(i, j); TEMPLATE_CONST(true)(i, j); } </code></pre>
 

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