12345678910111213141516171819202122232425262728293031323334 | #include <memory>
class bar;
class foo
{
public:
foo(const std::shared_ptr<bar>& b)
: forward_reference{b}
{ }
private:
std::shared_ptr<bar> forward_reference;
};
class bar
{
public:
void set_back_reference(const std::weak_ptr<foo>& f)
{
this->back_reference = f;
}
void do_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 →
Requires
c++11
or newer
Intent
Maintain a non-owning reference to a shared dynamically allocated
object to break circular dependencies.
Description
The std::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
Requires
c++11
or newer
Copy elements from a range to another range or container.
Requires
c++98
or newer
Count the number of occurrences of a particular value in a range of
elements.
Requires
c++11
or newer
Sort elements in a range into a given order.
Requires
c++98
or newer
Swap the contents of two containers.
Requires
c++98
or newer
Swap the values of two objects.
Classes
Requires
c++11
or newer
Implement the assignment operator with strong exception safety.
Requires
c++98
or newer
Delegate behavior to derived classes without incurring the cost of
run-time polymorphism.
Requires
c++11
or newer
Implement a lexicographic ordering over class members.
Requires
c++98
or newer
Reduce dependencies on internal class details and improve
encapsulation.
Requires
c++11
or newer
Remove compilation dependencies on internal class implementations
and improve compile times.
Requires
c++11
or newer
Safely and efficiently implement RAII to encapsulate the
management of dynamically allocated resources.
Requires
c++98
or newer
Utilise the value semantics of existing types to avoid having to
implement custom copy and move operations.
Requires
c++11
or newer
Create a copy of an object through a pointer to its base type.
Concurrency
Requires
c++11
or newer
Execute code on a separate thread.
Requires
c++11
or newer
High-level asynchronous execution of tasks.
Requires
c++11
or newer
Use promises to communicate values between threads.
Containers
Requires
c++11
or newer
Check if a particular key is in an associative container.
Requires
c++11
or newer
Use the erase-remove idiom to remove elements from a container.
Functions
Requires
c++14
or newer
Unpack a tuple as the arguments of a function.
Requires
c++17
or newer
Allow argument values to be omitted when calling a function.
Requires
c++11
or newer
Pass fixed-size arrays to and from functions.
Requires
c++11
or newer
Return multiple values of different types from a function.
Input streams
Requires
c++98
or newer
Process the contents of an input stream line-by-line.
Requires
c++98
or newer
Read a sequence of delimited values from a single line of an
input stream into a standard container.
Requires
c++98
or newer
Ensure that multiple stream reads are successful before using the
extracted values.
Memory management
Requires
c++11
or newer
Share ownership of a dynamically allocated object with another
unit of code.
Requires
c++11
or newer
Transfer unique ownership of a dynamically allocated object to
another unit of code.
Requires
c++98
or newer
Avoid manual memory management to improve safety and reduce bugs
and memory leaks.
Requires
c++11
or newer
Maintain a non-owning reference to a shared dynamically allocated
object to break circular dependencies.
Output streams
Requires
c++98
or newer
Write your class type objects to an output stream.
Requires
c++98
or newer
Align data in columns when writing to an output stream.
Random number generation
Requires
c++11
or newer
Choose a random element from a container.
Requires
c++11
or newer
Generate a random boolean value according to a bernoulli
distribution.
Requires
c++11
or newer
Generate a random integer according to a uniform distribution.
Requires
c++11
or newer
Seed a random number engine with greater unpredictability.
Ranges
Requires
c++11
or newer
Implement algorithms that can be applied to any generic range of
elements.
Requires
c++11
or newer
Iterate over a range of elements without using iterators or
indices.
Templates
Requires
c++11
or newer
Conditionally instantiate a class template depending on the
template arguments.
Requires
c++11
or newer
Conditionally instantiate a function template depending on the
template arguments.
Requires
c++11
or newer
Forward arguments of one function to another as though the wrapped
function had been called directly.
Time
Requires
c++11
or newer
Block the execution of a thread until a fixed point in time.
Requires
c++98
or newer
Measure the execution time of a unit of code.
Requires
c++11
or newer
Block the execution of a thread for a given amount of time.
Behavioral
Requires
c++11
or newer
Notify generic observer objects when an event occurs.
Requires
c++98
or newer
Separate generic algorithms from the elements or structure on which
they operate.
Creational
Requires
c++98
or newer
Separate the complex construction of an object from its
representation.
Requires
c++98
or newer
Extend the functionality of a class.
← All patterns
Search Results