maliput
|
Wraps an underlying type T such that its storage is a direct member field of this object (i.e., without any indirection into the heap), but unlike most member fields T's destructor is never invoked.
This is especially useful for function-local static variables that are not trivially destructable. We shouldn't call their destructor at program exit because of the "indeterminate order of ... destruction" as mentioned in cppguide's Static and Global Variables ** section, but other solutions to this problem place the objects on the heap through an indirection.
** This project follows rules listed in https://google.github.io/styleguide/cppguide.html but for the sake of limiting the number of editions to the copy, we delegate the decision to authors and reviewers discretion.
Compared with other approaches, this mechanism more clearly describes the intent to readers, avoids "possible leak" warnings from memory-checking tools, and is probably slightly faster.
Example uses:
The singleton pattern:
A lookup table, created on demand the first time its needed, and then reused thereafter:
#include <include/maliput/common/maliput_never_destroyed.h>
Public Member Functions | |
template<typename... Args> | |
never_destroyed (Args &&... args) | |
Passes the constructor arguments along to T using perfect forwarding. More... | |
~never_destroyed ()=default | |
Does nothing. Guaranteed! More... | |
T & | access () |
Returns the underlying T reference. More... | |
const T & | access () const |
|
explicit |
Passes the constructor arguments along to T using perfect forwarding.
|
default |
Does nothing. Guaranteed!
T& access | ( | ) |
Returns the underlying T reference.
const T& access | ( | ) | const |