Tag dispatching
is basically a name given to a technique employed to find correct overloaded function. So, technically it is nothing but overloading only.
To put it from Boost site:
Tag dispatching is a way of using function overloading to dispatch
based on properties of a type, and is often used hand in hand with
traits classes.
You can see it used all over in you standard library algorithm
header file. Just for the sake of an example, consider that there is an algorithm AlgoX
which can be performed a lot more efficiently on a container providing random access (eg vector
) than a container providing bidirectional access (list
). So, for selecting the algorithm based on the iterator type
one would use tag dispatching using iterator_traits
template <typename Iter>
void AlgoXImpl(Iter first, Iter last, bidirectional_iterator_tag) {
//.....Algo specialized to bidirectional iterators
}
template <typename Iter>
void AlgoXImpl(Iter first, Iter last, random_access_iterator_tag) {
//.....Algo specialized to random access iterators
}
template <typename Iter>
void AlgoX(Iter first, Iter last) {
if (first == last) return;
AlgoXImpl(first, last, typename iterator_traits<Iter>::iterator_category());
}
As you can see, to a simple mind this is nothing but an example of operator overloading as the categories are essentially different types.
For a more real world example, you can checkout out how std::rotate
is implemented.