C++ Tag Dispatching | Pattern Guide with Iterator and Serialization Tags
이 글의 핵심
Tag dispatching dispatches to different `*_impl` functions using tag types—classic STL pattern with iterator categories.
What is tag dispatching?
Use empty tag types and overload resolution to branch at compile time.
The Korean article demonstrates IntTag/FloatTag, iterator advanceImpl with input_iterator_tag vs random_access_iterator_tag, distance computation, serialization tags (PrimitiveTag, ContainerTag, CustomTag), trivial vs non-trivial copy tags, and small-array vs large-array sort selection.
if constexpr vs tag dispatching
- C++17+:
if constexprcan express many branches in one function. - C++11/14: tag dispatching is a natural fit.
- Complex branching: sometimes clearer as multiple
*_imploverloads.
Pitfalls
- Missing iterator tag dispatch → wrong
it += non non-random-access iterators. - Wrong
conditional_tpicking tags—double-check predicates.
FAQ
Performance: Both tag dispatch and if constexpr compile away—no inherent runtime difference.
Related posts
- Type traits
- Tag dispatch
- Expression templates
Keywords
C++, tag dispatch, metaprogramming, templates, STL.
See also
- Tag dispatch
- Expression templates
- SFINAE & Concepts
- Type traits
algorithm::copy