Home  >  Article  >  Backend Development  >  ## Can C 17 Achieve Partial Class Template Argument Deduction?

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-25 06:20:28261browse

## 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>

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!

Statement:
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