A textbook I have notes that you can provide your own implementation for standard library functions like swap(x,y)
via template specialization or function overloading. This would be useful for any types which can benefit from something other than an assignment swap, like STL containers for example (which already have swaps written, I know).
My questions are the following:
What's better: template specialization to give your specialized swap implementation, or function overloading providing the exact parameters you wish to use without a template?
Why is it better? Or if they're equal, why is this?
You generally use templates when you want to do the same set of operations on many different data types. You generally would use function overloading when you want to do different operations on certain sets of data.
You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.
The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization. A primary template is the template that is being specialized.
Function Template is the correct terminology (a template to instantiate functions from). Template Function is a colloquial synonym. So, there's no difference whatsoever.
Short story: overload when you can, specialise when you need to.
Long story: C++ treats specialisation and overloads very differently. This is best explained with an example.
template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*);
Now let's swap the last two.
template <typename T> void foo(T); template <> void foo<int*>(int*); // specialisation of foo(T) template <typename T> void foo(T*); // overload of foo(T) foo(new int); // calls foo(T*) !!!
The compiler does overload resolution before it even looks at specialisations. So, in both cases, overload resolution chooses foo(T*)
. However, only in the first case does it find foo<int*>(int*)
because in the second case the int*
specialisation is a specialisation of foo(T)
, not foo(T*)
.
You mentioned std::swap
. This makes things even more complicated.
The standard says that you can add specialisations to the std
namespace. Great, so you have some Foo
type and it has a performant swap then you just specialise swap(Foo&, Foo&)
in the std
namespace. No problems.
But what if Foo
is a template class? C++ doesn't have partial specialisation of functions, so you can't specialise swap
. Your only choice is overloading, but the standard says that you aren't allowed to add overloads into the std
namespace!
You have two options at this point:
Create a swap(Foo<T>&, Foo<T>&)
function in your own namespace, and hope that it gets found via ADL. I say "hope" because if the standard library calls swap like std::swap(a, b);
then ADL simply won't work.
Ignore the part of the standard that says not to add overloads and do it anyway. Honestly, even though it's technically not allowed, in all realistic scenarios it's going to work.
One thing to remember though is that there's no guarantee that the standard library uses swap
at all. Most algorithms use std::iter_swap
and in some implementations that I've looked at, it doesn't always forward to std::swap
.
There's little to add to Peter Alexander's answer. Let me just mention one use in wich function specialization could be prefearable over overloading: if you have to select among functions with no parameters.
E.g.
template<class T> T zero(); template<> int zero() { return 0; } template<> long zero() { return 0L; }
To do something similar using function overloading, you would have to add a parameter to the function signature:
int zero(int) { return 0; } long zero(long) { return 0L; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With