Drake
never_destroyed< T > Class Template Reference

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. More...

#include <drake/common/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
 
Does not allow copy, move, or assignment
 never_destroyed (const never_destroyed &)=delete
 
never_destroyedoperator= (const never_destroyed &)=delete
 
 never_destroyed (never_destroyed &&)=delete
 
never_destroyedoperator= (never_destroyed &&)=delete
 

Detailed Description

template<typename T>
class drake::never_destroyed< T >

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.

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.

Constructor & Destructor Documentation

never_destroyed ( const never_destroyed< T > &  )
delete
never_destroyed ( never_destroyed< T > &&  )
delete
never_destroyed ( Args &&...  args)
inlineexplicit

Passes the constructor arguments along to T using perfect forwarding.

Here is the call graph for this function:

~never_destroyed ( )
default

Does nothing. Guaranteed!

Here is the caller graph for this function:

Member Function Documentation

T& access ( )
inline

Returns the underlying T reference.

Here is the caller graph for this function:

const T& access ( ) const
inline
never_destroyed& operator= ( const never_destroyed< T > &  )
delete
never_destroyed& operator= ( never_destroyed< T > &&  )
delete

The documentation for this class was generated from the following file: