Why Was Partial Deduction Removed from Class Templates?
Nov 11, 2024 pm 01:55 PMDeduction 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);
In this scenario, if partial deduction were allowed, the expected deduction would be tuple<int, string, float>. However, tuple<int> 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]{}; };
And its helper function:
template <std::size_t S, typename T> test<S, T> helper(T (&amp;input)[S]) { return input; }
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
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.
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
