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

Recommended Answers

All 3 Replies

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

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.

commented: Could not have said it better! +13

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.