Home > Backend Development > C++ > Why Was Partial Deduction Removed from Class Templates?

Why Was Partial Deduction Removed from Class Templates?

Susan Sarandon
Release: 2024-11-11 13:55:03
Original
1026 people have browsed it

Why Was Partial Deduction Removed from Class Templates?

Deduction for Class Templates

Template argument deduction for class templates aims to streamline the behavior between function templates and class templates. However, the proposal initially included partial deduction, where only a subset of arguments would be explicitly specified.

Partial Deduction Concerns

The concern raised by Botond Ballo highlights the potential for confusion in cases where partial deduction can lead to ambiguous interpretations. For example:

tuple<int> t(42, "waldo", 2.0f);
Copy after login

In this scenario, if partial deduction were allowed, the expected deduction would be tuple. However, tuple is also a valid type, creating an ambiguity.

Current Behavior

Due to these concerns, partial deduction for class templates was removed from the proposal. Currently, deduction can only be applied to all template arguments or none.

Example

Consider the following class template:

template <std::size_t S, typename T>
struct test
{
    test(T (&amp;input)[size]) : data(input) {}
    type_t (&amp;data)[size]{};
};
Copy after login

And its helper function:

template <std::size_t S, typename T>
test<S, T> helper(T (&amp;input)[S]) { return input; }
Copy after login

In the given code:

int buffer[5];

auto a = helper<5, int>(buffer); // No deduction
auto b = helper<5>(buffer);      // Type deduced
auto c = helper(buffer);         // Type and size deduced
Copy after login

Only full deduction is allowed, as demonstrated by the error when attempting to deduce only the type:

auto b = helper<5>(buffer);      // Type deduced: FAILS.
Copy after login

The above is the detailed content of Why Was Partial Deduction Removed from Class Templates?. 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