std::optional<T>:: operator=
|
optional
&
operator
=
(
std::
nullopt_t
)
noexcept
;
|
(1) |
(C++17부터)
(C++20부터 constexpr) |
|
constexpr
optional
&
operator
=
(
const
optional
&
other
)
;
|
(2) | (C++17부터) |
|
constexpr
optional
&
operator
=
( optional && other ) noexcept ( /* 아래 참조 */ ) ; |
(3) | (C++17부터) |
|
template
<
class
U
>
optional & operator = ( const optional < U > & other ) ; |
(4) |
(C++17부터)
(C++20부터 constexpr) |
|
template
<
class
U
>
optional & operator = ( optional < U > && other ) ; |
(5) |
(C++17부터)
(C++20부터 constexpr) |
|
template
<
class
U
=
std::
remove_cv_t
<
T
>
>
optional & operator = ( U && value ) ; |
(6) |
(C++17부터)
(C++20부터 constexpr) |
* this 의 내용을 other 의 내용으로 대체합니다.
val
-
>
T
::
~T
(
)
를 호출합니다; 그렇지 않으면 아무런 효과도 없습니다.
*
this
는 이 호출 이후 값을 포함하지 않습니다.
| 효과 | * this 값 포함 | * this 값 미포함 |
|---|---|---|
| other 값 포함 |
|
|
| other 값 미포함 |
포함된 값을
val
-
>
T
::
~T
(
)
호출로 파괴
|
효과 없음 |
-
다음 12개의 값은 모두
false
[1]
입니다:
- std:: is_constructible_v < T, std:: optional < U > & >
- std:: is_constructible_v < T, const std:: optional < U > & >
- std:: is_constructible_v < T, std:: optional < U > && >
- std:: is_constructible_v < T, const std:: optional < U > && >
- std:: is_convertible_v < std:: optional < U > & , T >
- std:: is_convertible_v < const std:: optional < U > & , T >
- std:: is_convertible_v < std:: optional < U > && , T >
- std:: is_convertible_v < const std:: optional < U > && , T >
- std:: is_assignable_v < T & , std:: optional < U > & >
- std:: is_assignable_v < T & , const std:: optional < U > & >
- std:: is_assignable_v < T & , std:: optional < U > && >
- std:: is_assignable_v < T & , const std:: optional < U > && >
- 오버로드 (4) 의 경우, std:: is_constructible_v < T, const U & > 와 std:: is_assignable_v < T & , const U & > 가 모두 true 입니다.
- 오버로드 (5) 의 경우, std:: is_constructible_v < T, U > 와 std:: is_assignable_v < T & , U > 가 모두 true 입니다.
- std:: decay_t < U > (C++20 이전) std:: remove_cvref_t < U > (C++20 이후) 가 std:: optional < T > 가 아닙니다.
- std:: is_constructible_v < T, U > 가 true 입니다.
- std:: is_assignable_v < T & , U > 가 true 입니다.
-
다음 조건 중 하나 이상이 만족됩니다:
-
T가 스칼라 타입 이 아닙니다. -
std::
decay_t
<
U
>
가
T가 아닙니다.
-
-
↑
달리 말하면,
T는 (const 한정이 가능한) std:: optional < U > 타입의 어떤 표현식으로부터도 생성 가능하거나, 변환 가능하거나, 할당 가능하지 않습니다.
목차 |
매개변수
| other | - |
포함된 값을 할당할 다른
optional
객체
|
| value | - | 포함된 값에 할당할 값 |
반환값
* this
예외
T
의 생성자나 할당 연산자에 의해 발생하는 모든 예외를 전파합니다. 예외가 발생할 경우,
*
this
(그리고
(
2-5
)
의 경우
other
)의 초기화 상태는 변경되지 않습니다. 즉, 객체가 값을 포함하고 있었다면 여전히 값을 포함하며, 그 반대의 경우도 마찬가지입니다.
value
의 내용과
*
this
및
other
의 포함된 값들은 예외가 발생한 연산(복사 생성자, 이동 할당 등)의 예외 안전성 보장에 따라 달라집니다.
std:: is_nothrow_move_constructible_v < T > )
참고 사항
선택적 객체
op
는 다음 두 가지 방법으로 빈 optional로 변환될 수 있습니다:
op
=
{
}
;
와
op
=
nullopt
;
. 첫 번째 표현식은
{
}
를 사용하여 빈
optional
객체를 생성하고 이를
op
에 할당합니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_optional
|
202106L
|
(C++20)
(DR20) |
완전한 constexpr ( 1 ) , ( 4-6 ) |
예제
#include <iostream> #include <optional> int main() { std::optional<const char*> s1 = "abc", s2; // constructor s2 = s1; // assignment s1 = "def"; // decaying assignment (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
출력:
abc def
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3886 | C++17 |
오버로드
(
6
)
의 기본 템플릿 인수가
T
였음
|
std:: remove_cv_t < T > 로 변경됨 |
| P0602R4 | C++17 |
복사/이동 할당 연산자가 기본 연산이 trivial임에도
trivial하지 않을 수 있었음 |
triviality 전파 요구됨 |
| P2231R1 | C++20 | 오버로드 ( 1,4-6 ) 가 constexpr 가 아니었음 | constexpr 로 변경됨 |
참고 항목
|
포함된 값을 제자리에서 생성합니다
(public member function) |