| Technical Specification | ||||
| Filesystem library(filesystem TS) | ||||
| Library fundamentals(library fundamentals TS) | ||||
| Library fundamentals 2(library fundamentals TS v2) | ||||
| Library fundamentals 3(library fundamentals TS v3) | ||||
| Extensions for parallelism(parallelism TS) | ||||
| Extensions for parallelism 2(parallelism TS v2) | ||||
| Extensions for concurrency(concurrency TS) | ||||
| Extensions for concurrency 2(concurrency TS v2) | ||||
| Concepts(concepts TS) | ||||
| Ranges(ranges TS) | ||||
| Reflection(reflection TS) | ||||
| Mathematical special functions(special functions TR) | ||||
| Experimental Non-TS | ||||
| Pattern Matching | ||||
| Linear Algebra | ||||
| std::execution | ||||
| Contracts | ||||
| 2D Graphics |
| Member functions | ||||
| Observers | ||||
optional::value_or | ||||
| Modifiers | ||||
| Non-member functions | ||||
| Helper classes | ||||
| Helper objects | ||||
template<class U> constexpr T value_or( U&& default_value)const&; | (library fundamentals TS) | |
template<class U> constexpr T value_or( U&& default_value)&&; | (library fundamentals TS) | |
Returns the contained value if*this has a value, otherwise returnsdefault_value.
Contents |
| default_value | - | the value to use in case*this is empty |
| Type requirements | ||
-T must meet the requirements ofCopyConstructible in order to use overload (1). | ||
-T must meet the requirements ofMoveConstructible in order to use overload (2). | ||
-U&& must be convertible toT. | ||
The current value if*this has a value, ordefault_value otherwise.
Any exception thrown by the selected constructor of the return valueT.
#include <cstdlib>#include <experimental/optional>#include <iostream> std::experimental::optional<constchar*> maybe_getenv(constchar* n){if(constchar* x=std::getenv(n))return x;elsereturn{};} int main(){std::cout<< maybe_getenv("MYPWD").value_or("(none)")<<'\n';}
Possible output:
(none)
| returns the contained value (public member function)[edit] |