Movatterモバイル変換


[0]ホーム

URL:


C++Patterns

Featured pattern:Weak reference

12345678910111213141516171819202122232425262728293031323334#include<memory>classbar;classfoo{public:foo(conststd::shared_ptr<bar>&b):forward_reference{b}{}private:std::shared_ptr<bar>forward_reference;};classbar{public:voidset_back_reference(conststd::weak_ptr<foo>&f){this->back_reference=f;}voiddo_something(){std::shared_ptr<foo>shared_back_reference=this->back_reference.lock();if(shared_back_reference){// Use *shared_back_reference}}private:std::weak_ptr<foo>back_reference;};
Continue reading →
Requiresc++11 or newer

Intent

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

Description

Thestd::weak_ptr type represents a non-owning reference to dynamically allocated object with shared ownership (std::shared_ptr). As they do not contribute to the reference count of the managed object they refer to, the object...

Continue reading →

All patterns

Algorithms

Copy a range of elements

Requiresc++11 or newer

Copy elements from a range to another range or container.

Count occurrences of value in a range

Requiresc++98 or newer

Count the number of occurrences of a particular value in a range ofelements.

Sort a range of elements

Requiresc++11 or newer

Sort elements in a range into a given order.

Swap containers

Requiresc++98 or newer

Swap the contents of two containers.

Swap values

Requiresc++98 or newer

Swap the values of two objects.

Classes

Copy-and-swap

Requiresc++11 or newer

Implement the assignment operator with strong exception safety.

Delegate behavior to derived classes

Requiresc++98 or newer

Delegate behavior to derived classes without incurring the cost ofrun-time polymorphism.

Lexicographic ordering

Requiresc++11 or newer

Implement a lexicographic ordering over class members.

Non-member non-friend interfaces

Requiresc++98 or newer

Reduce dependencies on internal class details and improveencapsulation.

The PIMPL idiom

Requiresc++11 or newer

Remove compilation dependencies on internal class implementationsand improve compile times.

The rule of five

Requiresc++11 or newer

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

The rule of zero

Requiresc++98 or newer

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

Virtual constructor

Requiresc++11 or newer

Create a copy of an object through a pointer to its base type.

Concurrency

Create a thread

Requiresc++11 or newer

Execute code on a separate thread.

Execute a task asynchronously

Requiresc++11 or newer

High-level asynchronous execution of tasks.

Pass values between threads

Requiresc++11 or newer

Use promises to communicate values between threads.

Containers

Check existence of a key

Requiresc++11 or newer

Check if a particular key is in an associative container.

Remove elements from a container

Requiresc++11 or newer

Use the erase-remove idiom to remove elements from a container.

Functions

Apply tuple to a function

Requiresc++14 or newer

Unpack a tuple as the arguments of a function.

Optional arguments

Requiresc++17 or newer

Allow argument values to be omitted when calling a function.

Pass arrays

Requiresc++11 or newer

Pass fixed-size arrays to and from functions.

Return multiple values

Requiresc++11 or newer

Return multiple values of different types from a function.

Input streams

Read line-by-line

Requiresc++98 or newer

Process the contents of an input stream line-by-line.

Read a line of values

Requiresc++98 or newer

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

Validate multiple reads

Requiresc++98 or newer

Ensure that multiple stream reads are successful before using theextracted values.

Memory management

Shared ownership

Requiresc++11 or newer

Share ownership of a dynamically allocated object with anotherunit of code.

Unique ownership

Requiresc++11 or newer

Transfer unique ownership of a dynamically allocated object toanother unit of code.

Use RAII types

Requiresc++98 or newer

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

Weak reference

Requiresc++11 or newer

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

Output streams

Overload operator<<

Requiresc++98 or newer

Write your class type objects to an output stream.

Write data in columns

Requiresc++98 or newer

Align data in columns when writing to an output stream.

Random number generation

Choose a random element

Requiresc++11 or newer

Choose a random element from a container.

Flip a biased coin

Requiresc++11 or newer

Generate a random boolean value according to a bernoullidistribution.

Roll a die

Requiresc++11 or newer

Generate a random integer according to a uniform distribution.

Unpredictable random numbers

Requiresc++11 or newer

Seed a random number engine with greater unpredictability.

Ranges

Range-based algorithms

Requiresc++11 or newer

Implement algorithms that can be applied to any generic range ofelements.

Range iteration

Requiresc++11 or newer

Iterate over a range of elements without using iterators orindices.

Templates

Class template SFINAE

Requiresc++11 or newer

Conditionally instantiate a class template depending on thetemplate arguments.

Function template SFINAE

Requiresc++11 or newer

Conditionally instantiate a function template depending on thetemplate arguments.

Perfect forwarding

Requiresc++11 or newer

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

Time

Fixed time step

Requiresc++11 or newer

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

Measure execution time

Requiresc++98 or newer

Measure the execution time of a unit of code.

Sleep

Requiresc++11 or newer

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

Behavioral

Observer

Requiresc++11 or newer

Notify generic observer objects when an event occurs.

Visitor

Requiresc++98 or newer

Separate generic algorithms from the elements or structure on whichthey operate.

Creational

Builder

Requiresc++98 or newer

Separate the complex construction of an object from itsrepresentation.

Decorator

Requiresc++98 or newer

Extend the functionality of a class.

← All patterns

Search Results


[8]ページ先頭

©2009-2026 Movatter.jp