Overloading a Function Template

Overloading a Function Template

If the function template you’re adding incorporates functionality that you already have, then you need to remove the old members first; otherwise the duplication will cause ambiguity. Here’s an example. Suppose you already have a template defined:

	template 	void func (type t_1, type t_2);

This allows you to make a call like func(3, 4) successfully. But what if you need to call func() for two different types of arguments (for instance func(4, 5.6))? The compiler will report it as an error because deduced types for the same template arguments are not the same for every occurrence in the function parameter list.

You might think about overloading it as follows:

	template 	void func (type_1 t_1, type_2 t_2);

Now, your second call func(4, 5.6) will be successfully resolved but the first call will be ambiguous because the new added member in the set of overloaded function templates can handle the first call as well?as type_1 and type_2 can also be deduced to the same type. So the compiler will find it ambiguous because it can not resolve to the best viable function among the two functions instantiated from both function templates.


Share the Post: