Show: Object Pascal C++
Display Preferences

Implicit And Explicit Template Functions

From Appmethod Topics
Jump to: navigation, search

Go Up to Function Templates Overview Index

When doing overload resolution (following the steps of looking for an exact match), the compiler ignores template functions that have been generated implicitly by the compiler.

template<class T> T max(T a, T b){
        return  (a > b) ? a : b;
}
void f(int i, char c)
{
    max(i, i);     // calls max(int ,int )
    max(c, c);     // calls max(char,char)
    max(i, c);     // no match for max(int,char)
    max(c, i);     // no match for max(char,int)
}

This code results in the following error messages:

Could not find a match for 'max(int,char)' in function f(int,char)Could not find a match for 'max(char,int)' in function f(int,char)
Could not find a match for 'max(char,int)' in function f(int,char)

If the user explicitly declares a function, this function, on the other hand, will participate fully in overload resolution. See the example of explicit function.

When searching for an exact match for template function parameters, trivial conversions are considered to be exact matches. See the example on trivial conversions.

Template functions with derived class pointer or reference arguments are permitted to match their public base classes. See the example of base class referencing.

Personal tools
In other languages