Backport
bpstd::optional< T > Class Template Reference

The class template optional manages an optional contained value, i.e. a value that may or may not be present. More...

#include <optional.hpp>

Public Types

using value_type = T
 The underlying type of this Optional.
 

Public Member Functions

 optional (detail::enable_overload_if_t< std::is_copy_constructible< T >::value, const optional & > other)
 Copy constructs an optional. More...
 
 optional (detail::disable_overload_if_t< std::is_copy_constructible< T >::value, const optional & > other)=delete
 
 optional (detail::enable_overload_if_t< std::is_move_constructible< T >::value, optional && > other)
 Move constructs an optional. More...
 
 optional (detail::disable_overload_if_t< std::is_move_constructible< T >::value, optional && > other)=delete
 
template<typename... Args, typename = enable_if_t<std::is_constructible<T,Args...>::value>>
constexpr optional (in_place_t, Args &&... args)
 Constructs an optional object that contains a value. More...
 
template<typename U , typename... Args, typename = enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args...>::value>>
constexpr optional (in_place_t, std::initializer_list< U > ilist, Args &&...args)
 Constructs an optional object that contains a value. More...
 
optional & operator= (nullopt_t)
 Assigns this optional to be a null optional. More...
 
optional & operator= (detail::enable_overload_if_t< std::is_copy_constructible< T >::value &&std::is_copy_assignable< T >::value, const optional & > other)
 Copy assigns the optional stored in other. More...
 
optional & operator= (detail::disable_overload_if_t< std::is_copy_constructible< T >::value &&std::is_copy_assignable< T >::value, const optional & > other)=delete
 
optional & operator= (detail::enable_overload_if_t< std::is_move_constructible< T >::value &&std::is_move_assignable< T >::value, optional && > other)
 Move assigns the optional stored in other. More...
 
optional & operator= (detail::disable_overload_if_t< std::is_move_constructible< T >::value &&std::is_move_assignable< T >::value, optional && > other)=delete
 
template<typename U = T, typename = enable_if_t<detail::optional_is_value_assignable<T,U>::value>>
optional & operator= (U &&value)
 Perfect-forwarded assignment. More...
 
template<typename U >
optional & operator= (enable_if_t< detail::optional_is_copy_convert_assignable< T, U >::value, const optional< U > & > other)
 Assigns the state of other. More...
 
template<typename U >
optional & operator= (enable_if_t< detail::optional_is_move_convert_assignable< T, U >::value, optional< U > && > other)
 Assigns the state of other. More...
 
constexpr operator bool () const noexcept
 Checks whether *this contains a value. More...
 
constexpr bool has_value () const noexcept
 Checks whether *this contains a value. More...
 
void swap (optional &other)
 Swaps the contents with those of other. More...
 
void reset () noexcept(std::is_nothrow_destructible< T >::value)
 If *this contains a value, destroy that value as if by value().T::~T(). More...
 
template<typename... Args>
void emplace (Args &&...args)
 Constructs the contained value in-place. More...
 
template<typename U , typename... Args>
void emplace (std::initializer_list< U > ilist, Args &&...args)
 Constructs the contained value in-place. More...
 
template<typename U , bpstd::enable_if_t< bpstd::detail::optional_is_copy_convertible< T, U >::value &&std::is_convertible< const U &, T >::value > * >
BPSTD_INLINE_VISIBILITY optional (const optional< U > &other)
 
template<typename U , bpstd::enable_if_t< bpstd::detail::optional_is_move_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * >
BPSTD_INLINE_VISIBILITY optional (optional< U > &&other)
 
template<typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY optional (in_place_t, Args &&...args)
 
template<typename U , typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY optional (in_place_t, std::initializer_list< U > ilist, Args &&...args)
 
template<typename U , bpstd::enable_if_t< bpstd::detail::optional_is_value_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * >
constexpr BPSTD_INLINE_VISIBILITY optional (U &&value)
 
template<typename U , typename >
BPSTD_INLINE_VISIBILITY bpstd::optional< T > & operator= (U &&value)
 
template<typename U >
constexpr BPSTD_INLINE_VISIBILITY bpstd::optional< T >::value_type value_or (U &&default_value) const &
 
template<typename U >
BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR bpstd::optional< T >::value_type value_or (U &&default_value) &&
 
template<typename... Args>
BPSTD_INLINE_VISIBILITY void emplace (Args &&...args)
 
template<typename U , typename... Args>
BPSTD_INLINE_VISIBILITY void emplace (std::initializer_list< U > ilist, Args &&...args)
 
constexpr optional () noexcept
 Constructs an optional that does not contain a value.
 
constexpr optional (nullopt_t) noexcept
 Constructs an optional that does not contain a value.
 
template<typename U , enable_if_t< detail::optional_is_copy_convertible< T, U >::value &&std::is_convertible< const U &, T >::value > * = nullptr>
 optional (const optional< U > &other)
 Converting copy constructor. More...
 
template<typename U , enable_if_t< detail::optional_is_copy_convertible< T, U >::value &&!std::is_convertible< const U &, T >::value > * = nullptr>
 optional (const optional< U > &other)
 Converting copy constructor. More...
 
template<typename U , enable_if_t< detail::optional_is_move_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * = nullptr>
 optional (optional< U > &&other)
 Converting move constructor. More...
 
template<typename U , enable_if_t< detail::optional_is_move_convertible< T, U >::value &&!std::is_convertible< U &&, T >::value > * = nullptr>
 optional (optional< U > &&other)
 Converting move constructor. More...
 
template<typename U = T, enable_if_t< detail::optional_is_value_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * = nullptr>
constexpr optional (U &&value)
 Constructs an optional object that contains a value. More...
 
template<typename U = T, enable_if_t< detail::optional_is_value_convertible< T, U >::value &&!std::is_convertible< U &&, T >::value > * = nullptr>
constexpr optional (U &&value)
 Constructs an optional object that contains a value. More...
 
BPSTD_CPP14_CONSTEXPR value_type * operator-> () noexcept
 Accesses the contained value. More...
 
constexpr const value_type * operator-> () const noexcept
 Accesses the contained value. More...
 
BPSTD_CPP14_CONSTEXPR value_type & operator* () &noexcept
 Accesses the contained value. More...
 
BPSTD_CPP14_CONSTEXPR value_type && operator* () &&noexcept
 Accesses the contained value. More...
 
constexpr const value_type & operator* () const &noexcept
 Accesses the contained value. More...
 
constexpr const value_type && operator* () const &&noexcept
 Accesses the contained value. More...
 
BPSTD_CPP14_CONSTEXPR value_type & value () &
 Returns the contained value. More...
 
BPSTD_CPP14_CONSTEXPR value_type && value () &&
 Returns the contained value. More...
 
const BPSTD_CPP14_CONSTEXPR value_type & value () const &
 Returns the contained value. More...
 
const BPSTD_CPP14_CONSTEXPR value_type && value () const &&
 Returns the contained value. More...
 
template<typename U >
constexpr value_type value_or (U &&default_value) const &
 Returns the contained value if *this has a value, otherwise returns default_value. More...
 
template<typename U >
BPSTD_CPP14_CONSTEXPR value_type value_or (U &&default_value) &&
 Returns the contained value if *this has a value, otherwise returns default_value. More...
 

Detailed Description

template<typename T>
class bpstd::optional< T >

The class template optional manages an optional contained value, i.e. a value that may or may not be present.

A common use case for optional is the return value of a function that may fail. As opposed to other approaches, such as std::pair<T,bool>, optional handles expensive to construct objects well and is more readable, as the intent is expressed explicitly.

Any instance of optional<T> at any given point in time either contains a value or does not contain a value.

If an optional<T> contains a value, the value is guaranteed to be allocated as part of the optional object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though the operator*() and operator->() are defined.

When an object of type optional<T> is contextually converted to bool, the conversion returns true if the object contains a value and false if it does not contain a value.

The optional object contains a value in the following conditions:

  • The object is initialized with a value of type T
  • The object is assigned from another optional that contains a value.
  • The object does not contain a value in the following conditions:
  • The object is default-initialized.
  • The object is initialized with a value of nullopt_t or an optional object that does not contain a value.
  • The object is assigned from a value of nullopt_t or from an optional that does not contain a value
Template Parameters
Tthe underlying type

Definition at line 53 of file optional.hpp.

Constructor & Destructor Documentation

◆ optional() [1/10]

template<typename T >
BPSTD_INLINE_VISIBILITY bpstd::optional< T >::optional ( detail::enable_overload_if_t< std::is_copy_constructible< T >::value, const optional< T > & >  other)
inline

Copy constructs an optional.

If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

If other does not contain a value, constructs an object that does not contain a value.

Note
This constructor is defined as deleted if std::is_copy_constructible_v<T> is false
Parameters
otherthe optional to copy

Definition at line 1084 of file optional.hpp.

◆ optional() [2/10]

template<typename T >
BPSTD_INLINE_VISIBILITY bpstd::optional< T >::optional ( detail::enable_overload_if_t< std::is_move_constructible< T >::value, optional< T > && >  other)
inline

Move constructs an optional.

If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other) and does not make other empty: a moved-from optional still contains a value, but the value itself is moved from.

If other does not contain a value, constructs an object that does not contain a value.

Note
This constructor is defined as deleted if std::is_move_constructible_v<T> is false
Parameters
otherthe optional to move

Definition at line 1096 of file optional.hpp.

◆ optional() [3/10]

template<typename T >
template<typename U , enable_if_t< detail::optional_is_copy_convertible< T, U >::value &&std::is_convertible< const U &, T >::value > * = nullptr>
bpstd::optional< T >::optional ( const optional< U > &  other)

Converting copy constructor.

If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) optional, i.e., the following 8 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
This constructor is explicit if and only if std::is_convertible_v<const U&, T> is false
Parameters
otherthe other type to convert

◆ optional() [4/10]

template<typename T >
template<typename U , enable_if_t< detail::optional_is_copy_convertible< T, U >::value &&!std::is_convertible< const U &, T >::value > * = nullptr>
bpstd::optional< T >::optional ( const optional< U > &  other)
explicit

Converting copy constructor.

If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) optional, i.e., the following 8 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
This constructor is explicit if and only if std::is_convertible_v<const U&, T> is false
Parameters
otherthe other type to convert

◆ optional() [5/10]

template<typename T >
template<typename U , enable_if_t< detail::optional_is_move_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * = nullptr>
bpstd::optional< T >::optional ( optional< U > &&  other)

Converting move constructor.

If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other).

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, U&&> is true
  • T is not constructible or convertible from any expression of type (possibly const) optional, i.e., the following 8 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false
Parameters
otherthe other optional to move

◆ optional() [6/10]

template<typename T >
template<typename U , enable_if_t< detail::optional_is_move_convertible< T, U >::value &&!std::is_convertible< U &&, T >::value > * = nullptr>
bpstd::optional< T >::optional ( optional< U > &&  other)
explicit

Converting move constructor.

If other doesn't contain a value, constructs an optional object that does not contain a value. Otherwise, constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other).

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, U&&> is true
  • T is not constructible or convertible from any expression of type (possibly const) optional, i.e., the following 8 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false
Parameters
otherthe other optional to move

◆ optional() [7/10]

template<typename T >
template<typename... Args, typename = enable_if_t<std::is_constructible<T,Args...>::value>>
constexpr bpstd::optional< T >::optional ( in_place_t  ,
Args &&...  args 
)
explicitconstexpr

Constructs an optional object that contains a value.

the value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T from the arguments std::forward<Args>(args)...

Parameters
argsthe arguments to pass to T's constructor

◆ optional() [8/10]

template<typename T >
template<typename U , typename... Args, typename = enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args...>::value>>
constexpr bpstd::optional< T >::optional ( in_place_t  ,
std::initializer_list< U >  ilist,
Args &&...  args 
)
explicitconstexpr

Constructs an optional object that contains a value.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T from the arguments std::forward<std::initializer_list<U>>(ilist), std::forward<Args>(args)...

Parameters
ilistAn initializer list of entries to forward
argsthe arguments to pass to T's constructor

◆ optional() [9/10]

template<typename T >
template<typename U = T, enable_if_t< detail::optional_is_value_convertible< T, U >::value &&std::is_convertible< U &&, T >::value > * = nullptr>
constexpr bpstd::optional< T >::optional ( U &&  value)
constexpr

Constructs an optional object that contains a value.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.

Note
This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr
This constructor does not participate in overload resolution unless std::is_constructible_v<T, U&&> is true and decay_t is neither std::in_place_t nor optional<T>.
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false
Parameters
valuethe value to copy

◆ optional() [10/10]

template<typename T >
template<typename U = T, enable_if_t< detail::optional_is_value_convertible< T, U >::value &&!std::is_convertible< U &&, T >::value > * = nullptr>
constexpr bpstd::optional< T >::optional ( U &&  value)
explicitconstexpr

Constructs an optional object that contains a value.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.

Note
This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr
This constructor does not participate in overload resolution unless std::is_constructible_v<T, U&&> is true and decay_t is neither std::in_place_t nor optional<T>.
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false
Parameters
valuethe value to copy

Member Function Documentation

◆ emplace() [1/2]

template<typename T >
template<typename... Args>
void bpstd::optional< T >::emplace ( Args &&...  args)

Constructs the contained value in-place.

If *this already contains a value before the call, the contained value is destroyed by calling its destructor.

Parameters
argsthe arguments to pass to the constructor

◆ emplace() [2/2]

template<typename T >
template<typename U , typename... Args>
void bpstd::optional< T >::emplace ( std::initializer_list< U >  ilist,
Args &&...  args 
)

Constructs the contained value in-place.

If *this already contains a value before the call, the contained value is destroyed by calling its destructor.

Parameters
ilistthe initializer list to pass to the constructor
argsthe arguments to pass to the constructor

◆ has_value()

template<typename T >
constexpr BPSTD_INLINE_VISIBILITY bool bpstd::optional< T >::has_value
inlineconstexprnoexcept

Checks whether *this contains a value.

Returns
true if *this contains a value, false if *this does not contain a value

Definition at line 1270 of file optional.hpp.

◆ operator bool()

template<typename T >
constexpr BPSTD_INLINE_VISIBILITY bpstd::optional< T >::operator bool
inlineexplicitconstexprnoexcept

Checks whether *this contains a value.

Returns
true if *this contains a value, false if *this does not contain a value

Definition at line 1262 of file optional.hpp.

◆ operator*() [1/4]

template<typename T >
BPSTD_CPP14_CONSTEXPR value_type&& bpstd::optional< T >::operator* ( ) &&
noexcept

Accesses the contained value.

Note
The behaviour is undefined if *this does not contain a value
Returns
a reference to the contained value

◆ operator*() [2/4]

template<typename T >
constexpr const BPSTD_INLINE_VISIBILITY bpstd::optional< T >::value_type && bpstd::optional< T >::operator*
inlineconstexprnoexcept

Accesses the contained value.

Note
The behaviour is undefined if *this does not contain a value
Returns
a reference to the contained value

Definition at line 1301 of file optional.hpp.

◆ operator*() [3/4]

template<typename T >
constexpr const value_type&& bpstd::optional< T >::operator* ( ) const &&
constexprnoexcept

Accesses the contained value.

Note
The behaviour is undefined if *this does not contain a value
Returns
a reference to the contained value

◆ operator*() [4/4]

template<typename T >
constexpr const value_type& bpstd::optional< T >::operator* ( ) const &
constexprnoexcept

Accesses the contained value.

Note
The behaviour is undefined if *this does not contain a value
Returns
a reference to the contained value

◆ operator->() [1/2]

template<typename T >
constexpr const value_type* bpstd::optional< T >::operator-> ( ) const
constexprnoexcept

Accesses the contained value.

Note
The behavior is undefined if *this does not contain a value.
Returns
a pointer to the contained value

◆ operator->() [2/2]

template<typename T >
constexpr const BPSTD_INLINE_VISIBILITY bpstd::optional< T >::value_type * bpstd::optional< T >::operator->
inlineconstexprnoexcept

Accesses the contained value.

Note
The behavior is undefined if *this does not contain a value.
Returns
a pointer to the contained value

Definition at line 1281 of file optional.hpp.

◆ operator=() [1/6]

template<typename T >
BPSTD_INLINE_VISIBILITY bpstd::optional< T > & bpstd::optional< T >::operator= ( detail::enable_overload_if_t< std::is_copy_constructible< T >::value &&std::is_copy_assignable< T >::value, const optional< T > & >  other)
inline

Copy assigns the optional stored in other.

Parameters
otherthe other optional to copy

Definition at line 1214 of file optional.hpp.

◆ operator=() [2/6]

template<typename T >
BPSTD_INLINE_VISIBILITY bpstd::optional< T > & bpstd::optional< T >::operator= ( detail::enable_overload_if_t< std::is_move_constructible< T >::value &&std::is_move_assignable< T >::value, optional< T > && >  other)
inline

Move assigns the optional stored in other.

Note
If other contained a value before the call, it is left as a null optional
Remarks
This assignment does not participate in overload resolution unless U is
Parameters
otherthe other optional to move

Definition at line 1230 of file optional.hpp.

◆ operator=() [3/6]

template<typename T >
template<typename U >
optional& bpstd::optional< T >::operator= ( enable_if_t< detail::optional_is_copy_convert_assignable< T, U >::value, const optional< U > & >  other)

Assigns the state of other.

If both *this and other do not contain a value, the function has no effect.

If *this contains a value, but other does not, then the contained value is destroyed by calling its destructor. *this does not contain a value after the call.

If other contains a value, then depending on whether *this contains a value, the contained value is either direct-initialized or assigned from *other.

Note
A moved-from optional still contains a value.
This overload does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, const U&> and std::is_assignable_v<T&, const U&> are both true.
  • T is not constructible, convertible, or assignable from any expression of type (possibly const) :optional, i.e., the following 12 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
    • std::is_assignable_v<T&, std::optional<U>&>
    • std::is_assignable_v<T&, const std::optional<U>&>
    • std::is_assignable_v<T&, std::optional<U>&&>
    • std::is_assignable_v<T&, const std::optional<U>&&>.
Parameters
otheranother optional object whose contained value to assign
Returns
reference to (*this)

◆ operator=() [4/6]

template<typename T >
template<typename U >
optional& bpstd::optional< T >::operator= ( enable_if_t< detail::optional_is_move_convert_assignable< T, U >::value, optional< U > && >  other)

Assigns the state of other.

If both *this and other do not contain a value, the function has no effect.

If *this contains a value, but other does not, then the contained value is destroyed by calling its destructor. *this does not contain a value after the call.

If other contains a value, then depending on whether *this contains a value, the contained value is either direct-initialized or assigned from std::move(*other).

Note
A moved-from optional still contains a value.
This overload does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<T, U&&> and std::is_assignable_v<T&, U&&> are both true.
  • T is not constructible, convertible, or assignable from any expression of type (possibly const) :optional, i.e., the following 12 type traits are all false:
    • std::is_constructible_v<T, std::optional<U>&>
    • std::is_constructible_v<T, const std::optional<U>&>
    • std::is_constructible_v<T, std::optional<U>&&>
    • std::is_constructible_v<T, const std::optional<U>&&>
    • std::is_convertible_v<std::optional<U>&, T>
    • std::is_convertible_v<const std::optional<U>&, T>
    • std::is_convertible_v<std::optional<U>&&, T>
    • std::is_convertible_v<const std::optional<U>&&, T>
    • std::is_assignable_v<T&, std::optional<U>&>
    • std::is_assignable_v<T&, const std::optional<U>&>
    • std::is_assignable_v<T&, std::optional<U>&&>
    • std::is_assignable_v<T&, const std::optional<U>&&>.
Parameters
otheranother optional object whose contained value to assign
Returns
reference to (*this)

◆ operator=() [5/6]

template<typename T >
BPSTD_INLINE_VISIBILITY bpstd::optional< T > & bpstd::optional< T >::operator= ( nullopt_t  )
inline

Assigns this optional to be a null optional.

If (*this) had a value before the call, the contained value is destroyed.

Definition at line 1205 of file optional.hpp.

◆ operator=() [6/6]

template<typename T >
template<typename U = T, typename = enable_if_t<detail::optional_is_value_assignable<T,U>::value>>
optional& bpstd::optional< T >::operator= ( U &&  value)

Perfect-forwarded assignment.

Depending on whether *this contains a value before the call, the contained value is either direct-initialized from std::forward<U>(value) or assigned from std::forward<U>(value).

Note
The function does not participate in overload resolution unless
  • decay_t is not optional<T>,
  • std::is_constructible_v<T, U> is true
  • std::is_assignable_v<T&, U> is true
  • and at least one of the following is true:
    • T is not a scalar type;
    • decay_t is not T.
Parameters
valueto assign to the contained value
Returns
reference to (*this)

◆ reset()

template<typename T >
BPSTD_INLINE_VISIBILITY void bpstd::optional< T >::reset
inlinenoexcept

If *this contains a value, destroy that value as if by value().T::~T().

Otherwise, there are no effects. *this does not contain a value after this call.

Definition at line 1428 of file optional.hpp.

◆ swap()

template<typename T >
BPSTD_INLINE_VISIBILITY void bpstd::optional< T >::swap ( optional< T > &  other)
inline

Swaps the contents with those of other.

Parameters
otherthe optional object to exchange the contents with

Definition at line 1411 of file optional.hpp.

◆ value() [1/4]

template<typename T >
BPSTD_INLINE_VISIBILITY const BPSTD_CPP14_CONSTEXPR bpstd::optional< T >::value_type && bpstd::optional< T >::value
inline

Returns the contained value.

Exceptions
bad_optional_accessif *this does not contain a value.
Returns
the value of *this

Definition at line 1339 of file optional.hpp.

◆ value() [2/4]

template<typename T >
BPSTD_CPP14_CONSTEXPR value_type&& bpstd::optional< T >::value ( ) &&

Returns the contained value.

Exceptions
bad_optional_accessif *this does not contain a value.
Returns
the value of *this

◆ value() [3/4]

template<typename T >
const BPSTD_CPP14_CONSTEXPR value_type& bpstd::optional< T >::value ( ) const &

Returns the contained value.

Exceptions
bad_optional_accessif *this does not contain a value.
Returns
the value of *this

◆ value() [4/4]

template<typename T >
const BPSTD_CPP14_CONSTEXPR value_type&& bpstd::optional< T >::value ( ) const &&

Returns the contained value.

Exceptions
bad_optional_accessif *this does not contain a value.
Returns
the value of *this

◆ value_or() [1/2]

template<typename T >
template<typename U >
BPSTD_CPP14_CONSTEXPR value_type bpstd::optional< T >::value_or ( U &&  default_value) &&

Returns the contained value if *this has a value, otherwise returns default_value.

Parameters
default_valuethe value to use in case *this is empty
Returns
the value to use in case *this is empty

◆ value_or() [2/2]

template<typename T >
template<typename U >
constexpr value_type bpstd::optional< T >::value_or ( U &&  default_value) const &
constexpr

Returns the contained value if *this has a value, otherwise returns default_value.

Parameters
default_valuethe value to use in case *this is empty
Returns
the value to use in case *this is empty

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