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 (std::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 | |
std::optional< std::reference_wrapper< T > > | opt_ |
A simple wrapper class for optional reference types.
Since std::optional (as of C++17 at least) does not support reference types (see [1]), the only way to use those is std::optional<std::reference_wrapper>. 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 does not rebind operator=() as std::optional does. Instead, assigning a value to this object will simply change the object to which it points. To change the value of the referred to object, use value() or one of the other operators.
[1] https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/
Definition at line 36 of file optional_reference.hpp.
|
default |
|
inline |
Definition at line 41 of file optional_reference.hpp.
|
inline |
Definition at line 46 of file optional_reference.hpp.
|
inline |
Definition at line 72 of file optional_reference.hpp.
|
inline |
Definition at line 92 of file optional_reference.hpp.
|
inline |
Definition at line 87 of file optional_reference.hpp.
|
inline |
Definition at line 66 of file optional_reference.hpp.
|
inline |
Returns a pointer to the referenced object or nullptr if no reference is held.
Definition at line 78 of file optional_reference.hpp.
|
inline |
Definition at line 51 of file optional_reference.hpp.
Referenced by utils::optional_reference< const unit_type >::operator*(), utils::optional_reference< const unit_type >::operator->(), and utils::optional_reference< const unit_type >::ptr().
|
private |
Definition at line 98 of file optional_reference.hpp.
Referenced by utils::optional_reference< const unit_type >::operator bool(), utils::optional_reference< const unit_type >::operator=(), utils::optional_reference< const unit_type >::ptr(), and utils::optional_reference< const unit_type >::value().