Policy Idiom

Learn about the C++ idiom policy.

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.

Press + to interact
template<class T, class Allocator = std::allocator<T>>
class vector;
template<class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class allocator = std::allocator<std::pair<const Key, T>>
class 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 ...