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);
In this scenario, if partial deduction were allowed, the expected deduction would be tuple
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 (&input)[size]) : data(input) {} type_t (&data)[size]{}; };
And its helper function:
template <std::size_t S, typename T> test<S, T> helper(T (&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!