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;}; |
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...
All patterns
Algorithms
Copy a range of elements
Copy elements from a range to another range or container.
Count occurrences of value in a range
Count the number of occurrences of a particular value in a range ofelements.
Classes
Delegate behavior to derived classes
Delegate behavior to derived classes without incurring the cost ofrun-time polymorphism.
Non-member non-friend interfaces
Reduce dependencies on internal class details and improveencapsulation.
The PIMPL idiom
Remove compilation dependencies on internal class implementationsand improve compile times.
The rule of five
Safely and efficiently implement RAII to encapsulate themanagement of dynamically allocated resources.
The rule of zero
Utilise the value semantics of existing types to avoid having toimplement custom copy and move operations.
Virtual constructor
Create a copy of an object through a pointer to its base type.
Concurrency
Pass values between threads
Use promises to communicate values between threads.
Containers
Check existence of a key
Check if a particular key is in an associative container.
Remove elements from a container
Use the erase-remove idiom to remove elements from a container.
Functions
Optional arguments
Allow argument values to be omitted when calling a function.
Return multiple values
Return multiple values of different types from a function.
Input streams
Read a line of values
Read a sequence of delimited values from a single line of aninput stream into a standard container.
Validate multiple reads
Ensure that multiple stream reads are successful before using theextracted values.
Memory management
Shared ownership
Share ownership of a dynamically allocated object with anotherunit of code.
Unique ownership
Transfer unique ownership of a dynamically allocated object toanother unit of code.
Use RAII types
Avoid manual memory management to improve safety and reduce bugsand memory leaks.
Weak reference
Maintain a non-owning reference to a shared dynamically allocatedobject to break circular dependencies.
Output streams
Random number generation
Flip a biased coin
Generate a random boolean value according to a bernoullidistribution.
Unpredictable random numbers
Seed a random number engine with greater unpredictability.
Ranges
Range-based algorithms
Implement algorithms that can be applied to any generic range ofelements.
Range iteration
Iterate over a range of elements without using iterators orindices.
Templates
Class template SFINAE
Conditionally instantiate a class template depending on thetemplate arguments.
Function template SFINAE
Conditionally instantiate a function template depending on thetemplate arguments.
Perfect forwarding
Forward arguments of one function to another as though the wrappedfunction had been called directly.