Policy Idiom
Explore the policy idiom in C++ that allows customizing class behavior via template parameters. Understand how policies work with defaults like hashing in containers, and see implementation techniques using composition and inheritance to achieve flexible and reusable code designs.
We'll cover the following...
Policy and traits are generally used together. We’ll start with policy, and then move on to discuss traits in the next lesson.
Policy
A policy is a generic function or class whose behavior can be configured. Typically, there are default values for the policy parameters, such as std::vector and std::unordered_map.
Let’s look at the code above. Each container has a default allocated for its elements, depending on the value of T (line 5) or std::pair<const Key, T> (line 8).
Additionally, ...