Home >Backend Development >C++ >## Can C 17 Achieve Partial Class Template Argument Deduction?
In C 17, Class Template Argument Deduction (CTAD) allows the deduction of template arguments based on the arguments passed to a function or constructor. However, this process currently requires either all or none of the arguments to be specified.
The question arises if it is possible to partially specify template arguments and have the remaining ones deduced. For example:
<code class="cpp">Base<V = bool> val1(1, 4.); // U & V deduced --> Base<int, double, bool> Base<T = bool, T =int> val2(5.); // V deduced --> Base<bool, int, double></code>
However, attempting to use alias templates to achieve this, such as:
<code class="cpp">template<class T, class U> using Base2 = Base<T, U, double>; void func() { NewBase2 val(1, 2); }</code>
results in a compilation error.
Currently, CTAD does not support partial deduction. The paper P1021R0, which proposed this feature, has not been accepted. However, C 20 may include support for alias templates (P1814) and aggregates (P1816), which could provide a workaround.
The above is the detailed content of ## Can C 17 Achieve Partial Class Template Argument Deduction?. For more information, please follow other related articles on the PHP Chinese website!