◐ Shell
clean mode source ↗

C++ Patterns

All patterns

Copy-and-swap

Requires c++11 or newer

Implement the assignment operator with strong exception safety.

The PIMPL idiom

Requires c++11 or newer

Remove compilation dependencies on internal class implementations and improve compile times.

The rule of five

Requires c++11 or newer

Safely and efficiently implement RAII to encapsulate the management of dynamically allocated resources.

The rule of zero

Requires c++98 or newer

Utilise the value semantics of existing types to avoid having to implement custom copy and move operations.

Pass arrays

Requires c++11 or newer

Pass fixed-size arrays to and from functions.

Read a line of values

Requires c++98 or newer

Read a sequence of delimited values from a single line of an input stream into a standard container.

Shared ownership

Requires c++11 or newer

Share ownership of a dynamically allocated object with another unit of code.

Unique ownership

Requires c++11 or newer

Transfer unique ownership of a dynamically allocated object to another unit of code.

Use RAII types

Requires c++98 or newer

Avoid manual memory management to improve safety and reduce bugs and memory leaks.

Weak reference

Requires c++11 or newer

Maintain a non-owning reference to a shared dynamically allocated object to break circular dependencies.

Roll a die

Requires c++11 or newer

Generate a random integer according to a uniform distribution.

Range iteration

Requires c++11 or newer

Iterate over a range of elements without using iterators or indices.

Perfect forwarding

Requires c++11 or newer

Forward arguments of one function to another as though the wrapped function had been called directly.

Fixed time step

Requires c++11 or newer

Block the execution of a thread until a fixed point in time.

Sleep

Requires c++11 or newer

Block the execution of a thread for a given amount of time.

Observer

Requires c++11 or newer

Notify generic observer objects when an event occurs.

Visitor

Requires c++98 or newer

Separate generic algorithms from the elements or structure on which they operate.

Builder

Requires c++98 or newer

Separate the complex construction of an object from its representation.

Decorator

Requires c++98 or newer

Extend the functionality of a class.