Tag Dispatching

Learn about tag dispatching techniques, and instantiate specific overloads.

Tag dispatching is a technique that enables us to select one or another function overload at compile time. It’s an alternative to std::enable_if and SFINAE and is simple to understand and use. The term “tag” describes an empty class that has no members (data), or functions (behavior). Such a class is only used to define a parameter (usually the last) of a function to decide whether to select it at compile-time, depending on the supplied arguments. To better understand this, let’s consider an example.

The standard library contains a utility function called std::advance that looks as follows:

Get hands-on with 1200+ tech skills courses.