SObjectizer  5.8
Loading...
Searching...
No Matches
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) noexcept
 
 outliving_reference_t (T &&)=delete
 
 outliving_reference_t (outliving_reference_t const &o) noexcept
 
template<typename U >
 outliving_reference_t (outliving_reference_t< U > const &o) noexcept
 
outliving_reference_toperator= (outliving_reference_t const &o)=delete
 
T & get () const 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));
...
}
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
outliving_reference_t< T > outliving_mutable(T &r)
Make outliving_reference wrapper for mutable reference.

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));
...
}
outliving_reference_t< const T > outliving_const(T const &r)
Make outliving_reference wrapper for const reference.
Attention
outliving_reference_t has no copy operator! It is CopyConstructible, but not CopyAssingable class.
Note
There is no implicit type conversion operator since v.5.8.0.
Template Parameters
Ttype for reference.
Since
v.5.5.19

Definition at line 97 of file outliving.hpp.

Member Typedef Documentation

◆ type

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

Definition at line 102 of file outliving.hpp.

Constructor & Destructor Documentation

◆ outliving_reference_t() [1/4]

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

Definition at line 104 of file outliving.hpp.

◆ 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)
inlinenoexcept

Definition at line 107 of file outliving.hpp.

◆ 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)
inlinenoexcept

Definition at line 112 of file outliving.hpp.

Member Function Documentation

◆ get()

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

Definition at line 118 of file outliving.hpp.

◆ 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

Definition at line 99 of file outliving.hpp.


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