Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Jun 21, 2004

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 <class type >
	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 <class type_1, class type_2>
	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.
Saurabh Ramya
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap