Policy-Based Class Design¶
A host class delegates selected design decisions to policy types chosen as template arguments.
Core Idea¶
In policy-based class design, a class template delegates selected behavior or structure to policy types supplied as template arguments. The host expresses the common work and calls on the operations or types it expects from each policy. A user selects suitable implementations when forming a specialization, so the same host design can support different decisions without rewriting its common code. A policy is an interface for a design choice; a policy class supplies one implementation of it.[1][2]
For example, a smart-pointer host can delegate checking and threading choices to separate policies. A host can inherit policy classes or contain policy objects; public inheritance is one technique, not the definition of a policy. Policy choices are made through template arguments, while actual operations may still have run-time state or costs.[1]
How would you explain it like I'm…
Snap-In Robot Parts
Plug-In Choices for Code
Template Policy Design
Structural Signature¶
- Host: a class template that coordinates common functionality and names policy parameters.
- Variation point: a behavioral or structural decision delegated to a policy.
- Policy requirements and implementations: operations, types, or other facilities expected by the host, and policy types providing them.
- Selection and specialization: template arguments select policy types for a particular host specialization.
- Interaction and constraints: the resulting choices must be suitable together for the intended use; the design may require extra checks or documentation.
Policies may be largely independent, which makes many combinations useful. This is a design goal, not a guarantee that every combination is valid. Boost describes an earlier iterator-adaptor design using non-orthogonal policies; the book also discusses compatibility of conversions between different policy-based specializations.[2][1]
What It Is Not¶
Policy-based design is a software technique, not the book Modern C++ Design, which describes it. It is not simply any configurable class: the specific idiom delegates choices through policy types used by a host. It is related to a run-time strategy object, but compile-time type selection and run-time replacement are distinct mechanisms and can have different trade-offs.
A compile-time error can reveal a missing operation when relevant code is instantiated. Successful compilation does not prove semantic compatibility, and compile-time selection does not guarantee inlining or zero overhead. Depending on the design, policy types may carry state or expose additional operations.[1]
Scope of Application¶
Policy classes can represent choices such as allocation, checking, storage or threading. The C++ standard allocator supplied to containers is a policy-class example noted by Boost.[2]
Clarity¶
Identify the host and the particular choice it delegates. State the policy's required operations and semantic expectations, then distinguish the policy interface from each implementation. Check which combinations and conversions are intended rather than inferring compatibility from names alone.
Manages Complexity¶
Moving a changeable choice into a policy can keep the host's common work separate from alternative implementations. The flexibility creates a trade-off: interfaces, interactions, error messages and the number of specializations can themselves be costly to understand or maintain.
Abstract Reasoning¶
Identify a design decision with more than one useful implementation; define what the host needs from a policy; implement compatible policy types; select them as template arguments; and validate the resulting specialization and its intended use. Not every policy needs to be reused or every possible combination supported.
Example¶
Alexandrescu illustrates a smart-pointer class template parameterized by a checking policy and a threading model. A particular specialization selects implementations of these choices, while the host's dereference operation can use their facilities.[1]
Neighborhood in Abstraction Space¶
Policy-Based Class Design sits in a sparse region of the domain-specific corpus (92nd percentile for distinctiveness): few abstractions share its structure, so a faithful description tends to retrieve it precisely.
Family — Optimization Algorithms & Methods (20 abstractions)
Nearest neighbors
- Automatic basis function construction — 0.78
- Automated planning and scheduling — 0.78
- Proto-value function — 0.78
- Generative design — 0.77
- Static single-assignment form — 0.77
Computed from structural-signature embeddings · 2026-11-14
Not to Be Confused With¶
Template instantiation is the operation of binding arguments into a template; policy-based design is an idiom for organizing variation points within a host class. The Strategy pattern is a related design idea, often discussed with run-time object substitution, but the two terms do not impose identical binding or object-lifetime rules. Modern C++ Design is the publication rather than the technique itself.
Book Context: Modern C++ Design¶
Andrei Alexandrescu's Modern C++ Design: Generic Programming and Design Patterns Applied was published by Addison-Wesley in 2001. Its first chapter presents policy-based class design, while the publisher's contents list also includes chapters on typelists and smart pointers among other topics. The publisher's preface describes a first part on techniques and a second part on generic components, and presents the associated Loki library in that context.[1][3]
The book is a publication that discusses this technique and other subjects; it is not another name for policy-based class design. A table of contents and sample chapter establish this bounded context, not a review of every chapter or a claim about the current availability or behavior of all associated code.[3]
References¶
[1] Andrei Alexandrescu, “Policy-Based Class Design,” chapter 1 in Modern C++ Design: Generic Programming and Design Patterns Applied (Addison-Wesley, 2001), publisher sample chapter. registry ↩a ↩b ↩c ↩d ↩e ↩f
[2] David Abrahams, “Generic Programming Techniques,” section “Policy Classes,” Boost C++ Libraries, revised 18 August 2004, archived guide. registry ↩a ↩b ↩c
[3] Andrei Alexandrescu, Modern C++ Design: Generic Programming and Design Patterns Applied (Addison-Wesley, 2001), publisher description, contents and preface. registry ↩a ↩b