When the Types Don’t Quite Match
Now let’s go back to the caller that needs a return value and takes a functor of type RWTFunctor<double(char)>. Suppose the callee needs to supply this function:
int function4 (int i);
The difference between the types of the functor invocation arguments and those of the function may look like a problem, but it really isn’t. Functors have some type flexibility, and the compiler does the conversions.
The types of the function’s arguments and return value must be compatible with the functor’s, but they do not have to match exactly. Compatibility requires the types to be convertible. Often the conversion is type promotion, which means the more specific type is promoted to a more general type as the value moves through the process. Keep in mind that argument values and return values are passed in opposite directions.
Arguments — The functor’s argument types must be convertible to the function’s argument types, because the functor passes its arguments to the function.
Return value — The function’s return type must be convertible to the functor’s return type, because the function passes its return value back to the functor.
At compile time, the functor’s char argument is promoted to the int that the function expects. The function’s int return value is promoted to the functor’s double. The caller calls the functor in the usual way:
functor4('y');
and the functor calls the function:
function4(int('y'));
The process looks something like
Figure 49.