A simple wrapper class for optional reference types. More...
#include <optional_reference.hpp>
Public Member Functions | |
optional_reference ()=default | |
optional_reference (T &ref) | |
optional_reference (utils::nullopt_t) | |
T & | value () const |
optional_reference< T > & | operator= (T &new_ref) |
operator bool () const | |
T * | ptr () const |
Returns a pointer to the referenced object or nullptr if no reference is held. More... | |
T * | operator-> () const |
T & | operator* () const |
Private Attributes | |
utils::optional< std::reference_wrapper< T > > | opt_ |
A simple wrapper class for optional reference types.
Since utils::optional (as of C++17 at least) does not support reference types (see [1]), the only way to use those is utils::optional<std::reference_wrapper<T>>. However, this makes the interace messy, as to access the referenced object you need an extra get() call to access the value stored in the reference wrapper.
This rebinds operator=() as boost::optional does. Assigning a value to this wrapper will simply change the object to which it points instead of assigning a value to the referenced object. To change the value of the referenced object, perform an assignment on value() or operator*.
[1] https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/
Definition at line 38 of file optional_reference.hpp.
|
default |
|
inline |
Definition at line 43 of file optional_reference.hpp.
|
inline |
Definition at line 48 of file optional_reference.hpp.
|
inlineexplicit |
Definition at line 74 of file optional_reference.hpp.
References utils::optional_reference< T >::opt_.
|
inline |
Definition at line 94 of file optional_reference.hpp.
References utils::optional_reference< T >::value().
|
inline |
Definition at line 89 of file optional_reference.hpp.
References utils::optional_reference< T >::value().
|
inline |
Definition at line 68 of file optional_reference.hpp.
References utils::optional_reference< T >::opt_.
|
inline |
Returns a pointer to the referenced object or nullptr if no reference is held.
Definition at line 80 of file optional_reference.hpp.
References utils::optional_reference< T >::opt_, and utils::optional_reference< T >::value().
|
inline |
Definition at line 53 of file optional_reference.hpp.
References utils::optional_reference< T >::opt_.
Referenced by gui2::builder_grid::build(), utils::optional_reference< T >::operator*(), utils::optional_reference< T >::operator->(), and utils::optional_reference< T >::ptr().
|
private |
Definition at line 100 of file optional_reference.hpp.
Referenced by utils::optional_reference< T >::operator bool(), utils::optional_reference< T >::operator=(), utils::optional_reference< T >::ptr(), and utils::optional_reference< T >::value().