Home >Backend Development >C++ >How Does C 11's Partial Ordering Determine More Specialized Function Templates?
Partial Ordering in Function Template Deduction
Problem:
The C 11 standard describes a partial ordering procedure for comparing the specialization of function templates, but its meaning is not entirely clear. Can you provide an explanation with examples?
Answer:
To determine if one function template is more specialized than another, the standard uses a partial ordering procedure. This procedure involves creating transformed function types for each template and comparing them to determine which template is more specialized.
Step 1: Create Transformed Function Types
For each template, a transformed function type is created by replacing type parameters with specific types (called synthesized types). These synthesized types are unique and not used elsewhere in the code.
Step 2: Match Transformed Types to Original Templates
The transformed function type of the first template is matched against the original function type of the second template. The process is then repeated in the opposite direction.
Step 3: Determine Specialization
If one transformation produces a match and the other does not, the template with the matched transformation is considered more specialized. If no match is found in either direction, neither template is considered more specialized than the other.
Example:
Consider two function templates:
template<typename T, typename U> void foo(T, U); // #1 template<typename T> void foo(T const*, X<T>); // #2
Matching the transformed function type of #1b (void foo(int, bool)) against the original function type of #2 (#2) fails. However, matching the transformed function type of #2b (void foo(char const*, X
Therefore, function template #2 is more specialized than function template #1.
Additional Note:
This partial ordering procedure is also used to compare the specialization of partial class template specializations. In this case, the procedure involves first creating fictitious function templates for each specialization and then comparing these function templates using the same rules.
The above is the detailed content of How Does C 11's Partial Ordering Determine More Specialized Function Templates?. For more information, please follow other related articles on the PHP Chinese website!