std::experimental::optional<T>::value_or (3) - Linux Manuals
std::experimental::optional<T>::value_or: std::experimental::optional<T>::value_or
Command to display std::experimental::optional<T>::value_or
manual in Linux: $ man 3 std::experimental::optional<T>::value_or
NAME
std::experimental::optional<T>::value_or - std::experimental::optional<T>::value_or
Synopsis
template< class U > (library fundamentals TS)
constexpr T value_or( U&& default_value ) const&;
template< class U > (library fundamentals TS)
constexpr T value_or( U&& default_value ) &&;
Returns the contained value if *this has a value, otherwise returns default_value.
1) Equivalent to bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value))
2) Equivalent to bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value))
Parameters
default_value - the value to use in case *this is empty
Type requirements
-
T must meet the requirements of CopyConstructible in order to use overload
(1).
-
T must meet the requirements of MoveConstructible in order to use overload
(2).
-
U&& must be convertible to T
Return value
The current value if *this has a value, or default_value otherwise.
Exceptions
Any exception thrown by the selected constructor of the return value T.
Example
// Run this code
#include <experimental/optional>
#include <iostream>
#include <cstdlib>
std::experimental::optional<const char*> maybe_getenv(const char* n)
{
if(const char* x = std::getenv(n))
return x;
else
return {};
}
int main()
{
std::cout << maybe_getenv("MYPWD").value_or("(none)") << '\n';
}
Possible output:
(none)
See also
returns the contained value
value (public member function)
Pages related to std::experimental::optional<T>::value_or
- std::experimental::optional<T>::value (3) - std::experimental::optional<T>::value
- std::experimental::optional<T>::emplace (3) - std::experimental::optional<T>::emplace
- std::experimental::optional<T>::operator->,std::experimental::optional<T>::operator* (3) - std::experimental::optional<T>::operator->,std::experimental::optional<T>::operator*
- std::experimental::optional<T>::operator= (3) - std::experimental::optional<T>::operator=
- std::experimental::optional<T>::operatorbool (3) - std::experimental::optional<T>::operatorbool
- std::experimental::optional<T>::optional (3) - std::experimental::optional<T>::optional
- std::experimental::optional<T>::swap (3) - std::experimental::optional<T>::swap
- std::experimental::optional<T>::~optional (3) - std::experimental::optional<T>::~optional
- std::experimental::optional (3) - std::experimental::optional
- std::experimental::observer_ptr (3) - std::experimental::observer_ptr
- std::experimental::observer_ptr<W>::get (3) - std::experimental::observer_ptr<W>::get
- std::experimental::observer_ptr<W>::observer_ptr (3) - std::experimental::observer_ptr<W>::observer_ptr
- std::experimental::observer_ptr<W>::operator*, (3) - std::experimental::observer_ptr<W>::operator*,
- std::experimental::observer_ptr<W>::operator*,std::experimental::observer_ptr<W>::operator-> (3) - std::experimental::observer_ptr<W>::operator*,std::experimental::observer_ptr<W>::operator->
- std::experimental::observer_ptr<W>::operatorbool (3) - std::experimental::observer_ptr<W>::operatorbool
- std::experimental::observer_ptr<W>::operatorelement_type* (3) - std::experimental::observer_ptr<W>::operatorelement_type*
- std::experimental::observer_ptr<W>::release (3) - std::experimental::observer_ptr<W>::release
- std::experimental::observer_ptr<W>::reset (3) - std::experimental::observer_ptr<W>::reset
- std::experimental::observer_ptr<W>::swap (3) - std::experimental::observer_ptr<W>::swap
- std::experimental::ostream_joiner (3) - std::experimental::ostream_joiner
- std::experimental::ostream_joiner<DelimT,CharT,Traits>::operator++ (3) - std::experimental::ostream_joiner<DelimT,CharT,Traits>::operator++
- std::experimental::ostream_joiner<DelimT,CharT,Traits>::operator= (3) - std::experimental::ostream_joiner<DelimT,CharT,Traits>::operator=
- std::experimental::ostream_joiner<DelimT,CharT,Traits>::ostream_joiner (3) - std::experimental::ostream_joiner<DelimT,CharT,Traits>::ostream_joiner
- std::experimental::overaligned_tag,std::experimental::overaligned (3) - std::experimental::overaligned_tag,std::experimental::overaligned