Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Jun 3, 2004



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use Explicit Template Argument to Solve Template Argument Deduction

The statement below will cause ambiguity because it is violating a rule for template argument deduction. That rule is: if a template parameter is used more than once in the function parameter list, then each deduced type for its template argument must match the first deduced type.

template <class type>
	void func (type t1, type t2);
	func(4, 5.6);
As you can see, the code is deducing two different types of the template argument for the template parameter 'type'. You can resolve this situation by using Explicit Template Argument:

	func<int>(4, 5.6)
Now the template argument for "type" is explicitly specified to "int". Thus the type of template parameter is fixed and does not depend on implicit template argument deduction anymore.

Saurabh Ramya
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date