SObjectizer  5.5
Public Types | Public Member Functions | Private Attributes | List of all members
so_5::outliving_reference_t< T > Class Template Reference

Helper class for indication of long-lived reference via its type. More...

#include <outliving.hpp>

Public Types

using type = T
 

Public Member Functions

 outliving_reference_t (T &r) SO_5_NOEXCEPT
 
 outliving_reference_t (T &&)=delete
 
 outliving_reference_t (outliving_reference_t const &o) SO_5_NOEXCEPT
 
template<typename U >
 outliving_reference_t (outliving_reference_t< U > const &o) SO_5_NOEXCEPT
 
outliving_reference_toperator= (outliving_reference_t const &o)=delete
 
 operator T & () const SO_5_NOEXCEPT
 
T & get () const SO_5_NOEXCEPT
 

Private Attributes

T * m_ptr
 

Detailed Description

template<typename T>
class so_5::outliving_reference_t< T >

Helper class for indication of long-lived reference via its type.

Sometimes it is necessary to store a reference to an object that lives longer that a reference holder. For example:

class config {...};
class config_consumer {
config & cfg_;
public :
config_consumer(config & cfg) : cfg_(cfg) {...}
...
};
void f() {
config cfg = load_config(); // cfg outlives all other objects in f().
config_consumer consumer(cfg); // It is safe to store a reference
// for all lifetime of consumer object.
...
}

The problem there is: when we see consumer::consumer(cfg) we can say is it safe to pass a reference to short-lived object to it or not. Helper class outliving_reference_t can be used as indicator that consumer::consumer expects a reference to long-lived object:

class config_consumer {
public :
: cfg_(cfg)
{...}
...
};
void f() {
config cfg = load_config();
// An explicit sign that cfg will outlive consumer object.
config_consumer consumer(so_5::outliving_mutable(cfg));
...
}

If it is necessary to store a const reference to long-lived object then outliving_reference_t<const T> and outliving_const() should be used:

class data_processor {
public :
: cfg_(cfg)
{...}
...
};
void f() {
config cfg = load_config();
config_consumer consumer(so_5::outliving_mutable(cfg));
data_processor processor(so_5::outliving_const(cfg));
...
}
Attention
outliving_reference_t has no copy operator! It is CopyConstructible, but not CopyAssingable class.
Template Parameters
Ttype for reference.
Since
v.5.5.19

Member Typedef Documentation

◆ type

template<typename T>
using so_5::outliving_reference_t< T >::type = T

Constructor & Destructor Documentation

◆ outliving_reference_t() [1/4]

template<typename T>
so_5::outliving_reference_t< T >::outliving_reference_t ( T &  r)
inlineexplicit

◆ outliving_reference_t() [2/4]

template<typename T>
so_5::outliving_reference_t< T >::outliving_reference_t ( T &&  )
delete

◆ outliving_reference_t() [3/4]

template<typename T>
so_5::outliving_reference_t< T >::outliving_reference_t ( outliving_reference_t< T > const &  o)
inline

◆ outliving_reference_t() [4/4]

template<typename T>
template<typename U >
so_5::outliving_reference_t< T >::outliving_reference_t ( outliving_reference_t< U > const &  o)
inline

Member Function Documentation

◆ get()

template<typename T>
T& so_5::outliving_reference_t< T >::get ( ) const
inline

◆ operator T &()

template<typename T>
so_5::outliving_reference_t< T >::operator T& ( ) const
inline

◆ operator=()

template<typename T>
outliving_reference_t& so_5::outliving_reference_t< T >::operator= ( outliving_reference_t< T > const &  o)
delete

Member Data Documentation

◆ m_ptr

template<typename T>
T* so_5::outliving_reference_t< T >::m_ptr
private

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