Benefits of sending a tag over normal overload resolution

Simple and simple: what is the advantage of sending a tag with normal overload resolution?

Are these both compilation processes and correct ones? So there should be no β€œwinner”, I suppose. And each dispatch tag should be rewritten / reorganized to a normal overload to some extent (possibly by adding several types), right?

Besides the way of working and selecting candidates, why do I prefer to send tags for congestion and in what cases?

+6
source share
2 answers

Tag dispatching is basically the name given to the method used to find the right overloaded function. Thus, technically it is only overload only.

To add it from the Boost site:

Sending tags is a way of using function overloads to send based on type properties, and is often used hand in hand with feature classes.

You can see that it is used throughout your standard library library algorithm . For example, consider that there is an AlgoX algorithm that can be run much more efficiently on a container that provides random access (for example, vector ) than a container that provides bi-directional access ( list ). Thus, to select an algorithm based on the iterator type , one could use tag dispatch with 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, for the simple mind, this is nothing more than an example of operator overloading, since categories are essentially different types.

For a more real-world example, you can check how std::rotate implemented.

+8
source

Tags can be associated with a type, including basic primitive types, using the appropriate attribute classes. For example, it would be impossible to make the pointer type a subclass of any iterative concept. However, a template feature class can associate it with the desired tag. Thus, label-based dispatching adds flexibility that allows you to build a dispatch scheme that should not yet be determined by the types involved.

+2
source

All Articles