Technical specifications
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals 2 TS)
Extensions for parallelism (parallelism TS)
Concepts (concepts TS)
Extensions for concurrency (concurrency TS)
Special mathematical functions (special math TR)
template< class U >
constexpr T value_or( U&& default_value ) const&;
(library fundamentals TS)
template< class U >
T value_or( U&& default_value ) &&;
(library fundamentals TS)

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


[edit] 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

[edit] Return value

The current value if *this has a value, or default_value otherwise.

[edit] Exceptions

Any exception thrown by the selected constructor of the return value T.

[edit] Example

#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;
       return {};
int main()
     std::cout << maybe_getenv("MYPWD").value_or("(none)") << '\n';

Possible output:


[edit] See also

returns the contained value
(public member function) [edit]