Home >Backend Development >C++ >How Does C Template Deduction Determine Specialization Using Partial Ordering?

How Does C Template Deduction Determine Specialization Using Partial Ordering?

Linda Hamilton
Linda HamiltonOriginal
2024-12-02 10:20:12277browse

How Does C   Template Deduction Determine Specialization Using Partial Ordering?

Partial Ordering Procedure in Template Deduction

The partial ordering procedure determines the specialization relationship between function templates in C template deduction. It involves creating transformed function types for each template and using them for comparison.

  1. Transformed Function Types:
    For each template, a transformed function type is created by substituting unique types for template parameters in its function type.
  2. Matching Arguments and Parameters:
    The transformed function type of one template is matched against the original function template of the other template, using the following two cases:

    • Transformed type-1 as argument template and original template-2 as parameter template
    • Transformed type-2 as argument template and original template-1 as parameter template
  3. Match Success and Ordering:
    If type deduction can successfully match the transformed function type to the original function template in either case, one template is more specialized than the other. If a successful match occurs in both directions, neither template is more specialized.

Example:

Consider these two function templates:

template<typename T, typename U>
void foo(T, U); // original #1

template<typename T>
void foo(T const*, X<T>); // original #2

Matching the transformed types:

  • 1b vs. #2: Type deduction cannot match parameters.

  • 2b vs. #1: Type deduction can match parameters (T = char const*, U = X).

Conclusion: Overload #2 is more specialized than #1.

The above is the detailed content of How Does C Template Deduction Determine Specialization Using Partial Ordering?. 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