Can anybody summarize the idea of function template overloading? What matters, template parameter or function parameter? What about the return value?
For example, g
There are two separate things here: function templating and function overloading. Any two distinct template declarations are likely to be overloads of each other, so your question doesn't quite make sense as stated. (The three "overloads" you give do not build upon the first template, rather you have four overloads to the same function name.) The real issue is, given some overloads and a call, how to call the desired overload?
First, the return type doesn't participate in the overloading process whether or not there is a template involved. So #2 will never play well with #1.
Second, the rules for function template overload resolution are different from the more commonly used class template specialization rules. Both essentially solve the same problem, but
You might be able to solve your particular problem with function template overloads, but you may have trouble fixing any bug that arises as the rules are longer and fewer people are familiar with their intricacies. I was unaware after a few years of template hacking that subtle function template overloading was even possible. In libraries such as Boost and GCC's STL, an alternative approach is ubiquitous. Use a templated wrapper class:
template< typename X, typename Y >
struct functor {
void operator()( X x, Y y );
};
template< typename X > // partial specialization:
struct functor< X, int > { // alternative to overloading for classes
void operator()( X x, int y );
};
Now you sacrifice the implicit instantiation syntax (with no angle brackets). If you want to get that back, you need another function
template< typename X, typename Y > void func( X x, Y y ) {
return functor< X, Y >()( x, y );
}
I'd be interested to hear whether function overloading can do anything (besides deduction) that class [partial] specialization can't…
And then, of course, your overload #3 will never face ambiguity because it has a different number of arguments than any other overload.