Home > Backend Development > C++ > body text

## Can C 17 Achieve Partial Class Template Argument Deduction?

Linda Hamilton
Release: 2024-10-25 06:20:28
Original
258 people have browsed it

## Can C  17 Achieve Partial Class Template Argument Deduction?

Partial Class Template Argument Deduction in C 17

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

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!