Skip to content

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

Imagine a toy robot kit where the main body is always the same, but you snap in different parts to choose how it acts — a careful part or a speedy part. You pick the parts when you build it, and the body does the rest. That's how some programmers build their code.

Plug-In Choices for Code

In some programming languages, you can write a template, which is like a blueprint with blanks to fill in. In policy-based class design, the main blueprint, called the host, does the shared work, but leaves certain decisions to smaller pieces called policies. When you use the blueprint, you pick which policy to plug into each blank. For example, a smart pointer could let you choose how it checks for mistakes and how it handles running many tasks at once. That way you get different versions without rewriting the main code.

Template Policy Design

Policy-based class design is a C++-style technique where a class template, the host, hands off certain behaviors or structures to policy types given as template arguments. The host contains the shared logic and calls operations or types it expects each policy to provide. A policy is the interface for a design decision, and a policy class is one particular implementation of it. When you create a specialization of the host, you pick suitable policy classes, so the same design can cover different choices without rewriting the common code. For example, a smart pointer host might delegate checking and threading to separate policies. The host can inherit from policy classes or hold policy objects; inheritance is one technique, not what defines a policy. The choice is made at compile time through template arguments, but the operations themselves may still have run-time state or costs.

 

In policy-based class design, a class template (the host) delegates selected aspects of its behavior or structure to policy types supplied as template parameters. The host implements the common logic and relies on operations or nested types it expects each policy to provide. A policy is an interface for one design dimension, essentially a set of requirements, while a policy class is a concrete implementation satisfying that interface. Users compose a specialization by choosing policy classes for each parameter, so a single host design supports many combinations of design decisions without modification of its common code. A standard example is a smart-pointer host that delegates checking strategy and threading model to separate policies. Mechanically, the host can inherit from policy classes or contain policy objects; public inheritance is a common technique but not definitional. The selection of policies is a compile-time choice made through template arguments, though the selected operations may still carry run-time state or costs.

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

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