Can the two features of C++ Function overloading and function templates be applied together ??
What i mean is ,typically function overloading is applied with functions having different arguments(i am in concern with equal number of arguments but ofcourse different build -in types)

Please provide me a simple example or else slight bit of explanation if that is possible

4 Years
Discussion Span
Last Post by G.CK

Can the two features of C++ Function overloading and function templates be applied together ??

Yes. You can define multiple functions with the same name even if one or all of the overloads are template functions. When you do this, the non-templated overloads will be considered first and if none of them fit, the most specific of the templated overloads is chosen. As an example take this:

template<class T>
void f(T x); // 1

template<class T>
void f(T* x); // 2

void f(int x); // 3
void f(int* x); // 4

Now if you call f with an int as an argument, overload 3 is chosen. With an int pointer overload 4 is chosen. With any other kind of pointer overload 2 is chosen. And with any argument that is not an int and not a pointer, overload 1 is chosen.

Look into template specialization. I think that may be what you're referring too.

No, don't. It's perfectly possible to overload templated function definitions using regular overloading as described above. In the majority of cases this is preferable to using template specialization.

For more information see Why Not Specialize Function Templates.

Could not have said it better!

You both are dead right .I totally forgot the existence of template specialisation .And even the example is perfect. Thanks a lot.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.