Partial Class Template Argument Deduction in C 17
Class Template Argument Deduction (CTAD) in C 17 enables the compiler to deduce template arguments from the provided arguments. While this feature allows for auto-deduction, is it possible to partially specify template arguments and leave the rest for the compiler to deduce?
Partial Deduction in C 17
Currently, CTAD requires all or nothing template argument specification. However, there have been attempts to introduce partial deduction through proposals such as P1021R0. While these proposals have not been accepted, support for alias templates (P1814) and aggregates (P1816) has been incorporated into the C 20 working draft.
Workarounds
In the absence of native partial deduction, here's a potential workaround:
<code class="cpp">template<class T, class U> using NewBase2 = Base<T, U, double>; // Usage void func() { NewBase2<bool, int> val(1, 2); }</code>
By introducing NewBase2 as an alias for Base, you can partially specify the template arguments (T and U) while allowing the compiler to deduce the remaining one (V) based on the usage in func().
Note: This workaround requires specifying the alias template arguments explicitly, which defeats the purpose of CTAD to some extent.
The above is the detailed content of Can C 17 Partially Deduct Template Arguments in Class Template Argument Deduction (CTAD)?. For more information, please follow other related articles on the PHP Chinese website!