Home > Backend Development > C++ > Can C 17 Partially Deduct Template Arguments in Class Template Argument Deduction (CTAD)?

Can C 17 Partially Deduct Template Arguments in Class Template Argument Deduction (CTAD)?

DDD
Release: 2024-10-25 06:38:02
Original
669 people have browsed it

Can C  17 Partially Deduct Template Arguments in Class Template Argument Deduction (CTAD)?

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template