std::expected<T,E>:: swap
|
기본 템플릿
|
||
|
constexpr
void
swap
(
expected
&
other
)
noexcept
(
/* see below */
)
;
|
(1) | (C++23부터) |
|
void
부분 특수화
|
||
|
constexpr
void
swap
(
expected
&
other
)
noexcept
(
/* see below */
)
;
|
(2) | (C++23부터) |
내용을 other 와 교환합니다.
has_value()
의 값
|
other. has_value ( ) 의 값 | |
|---|---|---|
| true | false | |
| true |
using
std::
swap
;
swap (
val
, rhs.
val
)
;
|
아래 참조 |
| false | other. swap ( * this ) ; |
using
std::
swap
;
swap (
unex
, rhs.
unex
)
;
|
has_value()
is
true
and
other.
has_value
(
)
is
false
, equivalent to:
// Case 1: 예상치 못한 값들의 이동 생성이 예외를 발생시키지 않는 경우:
// "other.unex"는 "other.val"의 생성이 실패할 경우 복원됩니다
if
constexpr
(
std::
is_nothrow_move_constructible_v
<
E
>
)
{
E temp
(
std
::
move
(
other.
unex
)
)
;
std::
destroy_at
(
std::
addressof
(
other.
unex
)
)
;
try
{
std::
construct_at
(
std::
addressof
(
other.
val
)
, std
::
move
(
val
)
)
;
// 예외를 발생시킬 수 있음
std::
destroy_at
(
std::
addressof
(
val
)
)
;
std::
construct_at
(
std::
addressof
(
unex
)
, std
::
move
(
temp
)
)
;
}
catch
(
...
)
{
std::
construct_at
(
std::
addressof
(
other.
unex
)
, std
::
move
(
temp
)
)
;
throw
;
}
}
// Case 2: expected 값의 이동 생성이 예외를 발생시키지 않는 경우:
// "this->val"은 "this->unex"의 생성이 실패할 경우 복원됩니다
else
{
T temp
(
std
::
move
(
val
)
)
;
std::
destroy_at
(
std::
addressof
(
val
)
)
;
try
{
std::
construct_at
(
std::
addressof
(
unex
)
, std
::
move
(
other.
unex
)
)
;
// 예외를 발생시킬 수 있음
std::
destroy_at
(
std::
addressof
(
other.
unex
)
)
;
std::
construct_at
(
std::
addressof
(
other.
val
)
, std
::
move
(
temp
)
)
;
}
catch
(
...
)
{
std::
construct_at
(
std::
addressof
(
val
)
, std
::
move
(
temp
)
)
;
throw
;
}
}
has_val
=
false
;
rhs.
has_val
=
true
;
- std:: is_swappable_v < T >
- std:: is_swappable_v < E >
- std:: is_move_constructible_v < T > && std:: is_move_constructible_v < E >
- std:: is_nothrow_move_constructible_v < T > || std:: is_nothrow_move_constructible_v < E >
has_value()
의 값
|
other. has_value ( ) 의 값 | |
|---|---|---|
| true | false | |
| true |
using
std::
swap
;
swap (
val
, rhs.
val
)
;
|
std::
construct_at
(
std::
addressof
(
unex
)
,
std
::
move
(
rhs.
unex
)
)
;
std:: destroy_at ( std:: addressof ( rhs.
unex
)
)
;
has_val
=
false
;
rhs.
has_val
=
true
;
|
| false | other. swap ( * this ) ; |
using
std::
swap
;
swap (
unex
, rhs.
unex
)
;
|
목차 |
매개변수
| other | - |
내용을 교환할
expected
객체
|
예외
std::
is_nothrow_move_constructible_v
<
T
>
&&
std::
is_nothrow_swappable_v
<
T
>
&&
std::
is_nothrow_move_constructible_v
<
E
>
&&
std::
is_nothrow_swappable_v
<
E
>
std::
is_nothrow_move_constructible_v
<
E
>
&&
std::
is_nothrow_swappable_v
<
E
>
예제
#include <expected> #include <iostream> #include <string_view> using Ex = std::expected<std::string, int>; void show(const Ex& ex1, const Ex& ex2, std::string_view term = "\n") { for (int i{}; i != 2; ++i) { std::cout << (i ? "ex2" : "ex1"); if (const Ex& ex = (i ? ex2 : ex1); ex.has_value()) std::cout << ".value() = " << *ex << " "; else std::cout << ".error() = " << ex.error() << " "; } std::cout << term; } int main() { Ex ex1("\N{CAT FACE}"); Ex ex2{"\N{GREEN HEART}"}; show(ex1, ex2, "after ex1.swap(ex2):\n"); ex1.swap(ex2); show(ex1, ex2, "\n\n"); ex2 = std::unexpected(13); show(ex1, ex2, "after ex1.swap(ex2):\n"); ex1.swap(ex2); show(ex1, ex2, "\n\n"); ex2 = std::unexpected(37); show(ex1, ex2, "after ex1.swap(ex2):\n"); ex1.swap(ex2); show(ex1, ex2); }
출력:
ex1.value() = 🐱 ex2.value() = 💚 after ex1.swap(ex2): ex1.value() = 💚 ex2.value() = 🐱 ex1.value() = 💚 ex2.error() = 13 after ex1.swap(ex2): ex1.error() = 13 ex2.value() = 💚 ex1.error() = 13 ex2.error() = 37 after ex1.swap(ex2): ex1.error() = 37 ex2.error() = 13
참고 항목
|
(C++23)
|
std::swap
알고리즘을 특수화함
(함수) |