std::experimental::optional<T>:: value_or
From cppreference.net
<
cpp
|
experimental
|
optional
|
template
<
class
U
>
constexpr T value_or ( U && default_value ) const & ; |
(라이브러리 펀더멘털 TS) | |
|
template
<
class
U
>
constexpr T value_or ( U && default_value ) && ; |
(라이브러리 펀더멘털 TS) | |
값이 포함되어 있으면 포함된 값을 반환하고, * this 그렇지 않으면 default_value 를 반환합니다.
1)
다음 표현과 동등함
bool
(
*
this
)
?
**
this
:
static_cast
<
T
>
(
std::
forward
<
U
>
(
default_value
)
)
.
2)
다음과 동일함
bool
(
*
this
)
?
std
::
move
(
**
this
)
:
static_cast
<
T
>
(
std::
forward
<
U
>
(
default_value
)
)
.
목차 |
매개변수
| default_value | - |
*this
가 비어 있을 경우 사용할 값
|
| 타입 요구사항 | ||
-
T
는 오버로드 (1)을 사용하기 위해
CopyConstructible
요구사항을 충족해야 합니다.
|
||
-
T
는 오버로드 (2)를 사용하기 위해
MoveConstructible
요구사항을 충족해야 합니다.
|
||
-
U&&
는
T
로 변환 가능해야 합니다.
|
||
반환값
현재 값은 * this 가 값을 가지는 경우 해당 값, 그렇지 않으면 default_value 입니다.
예외
반환값
T
의 선택된 생성자가 던지는 모든 예외.
예제
이 코드 실행
#include <cstdlib> #include <experimental/optional> #include <iostream> 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'; }
가능한 출력:
(none)
참고 항목
|
포함된 값을 반환합니다
(public member function) |