Home  >  Article  >  Backend Development  >  Why Can't We Partially Deduce Template Arguments for Class Templates?

Why Can't We Partially Deduce Template Arguments for Class Templates?

Barbara Streisand
Barbara StreisandOriginal
2024-11-08 21:13:02272browse

Why Can't We Partially Deduce Template Arguments for Class Templates?

Partial Template Argument Deduction for Class Templates: Revisited

Despite the efforts outlined in P0091 to unify function and class template behaviors, the possibility of partial argument deduction in class templates remains elusive. This discussion clarifies the current limitations and explores the potential reasons behind them.

Consider the class template test and its helper function helper. While helper allows partial deduction, as demonstrated by the provided code, the corresponding class template test does not.

template<std::size_t S, typename T>
struct test {
    static constexpr auto size = S;
    using type_t = T;
    test(type_t(&&input)[size]) : data(input) {}
    type_t(&&data)[size]{};
};
template<std::size_t S, typename T>
test<S, T> helper(T(&&input)[S]) { return input; }

Upon experimentation, it becomes apparent that class template deduction only occurs when all arguments are explicitly provided. This deviation from the expected behavior raises the question of whether there has been a misunderstanding in the interpretation of P0091.

As Botond Ballo's trip report suggests, partial deduction for class templates was proposed and later withdrawn due to concerns over confusion. For example, the following code would have caused a deduction to tuple, but tuple is a valid type on its own:

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

To avoid such ambiguities, the full set of template arguments must be provided for class templates, while partial deduction remains an option for function templates.

The above is the detailed content of Why Can't We Partially Deduce Template Arguments for Class Templates?. 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