Implicit Type Conversion and Template Deduction
When attempting to implicitly convert an int to a Scalar
In the provided code:
<code class="cpp">func(a, 2);</code>
the compiler attempts to implicitly convert the int 2 to a Scalar
<code class="cpp">func(a, Scalar<int>{2}); // C++14</code>
Alternatively, if C 17 is used, a deduction guide can be defined for Scalar, allowing the following syntax:
<code class="cpp">func(a, Scalar{2});</code>
Finally, explicit instantiation of the template function with the specified type argument can also bypass the need for implicit conversion:
<code class="cpp">func<int>(a, 2); // Assuming Scalar<T>::Scalar(T) is not explicit</code>
The above is the detailed content of Why Does Template Deduction Fail with Implicit Type Conversion for User-Defined Types?. For more information, please follow other related articles on the PHP Chinese website!