Namespaces
Variants

operator==, !=, <, <=, >, >=, <=> (std::optional)

From cppreference.net
Utilities library
헤더에 정의됨 <optional>
두 개의 optional 객체 비교
template < class T, class U >
constexpr bool operator == ( const optional < T > & lhs, const optional < U > & rhs ) ;
(1) (C++17부터)
template < class T, class U >
constexpr bool operator ! = ( const optional < T > & lhs, const optional < U > & rhs ) ;
(2) (C++17부터)
template < class T, class U >
constexpr bool operator < ( const optional < T > & lhs, const optional < U > & rhs ) ;
(3) (C++17부터)
template < class T, class U >
constexpr bool operator <= ( const optional < T > & lhs, const optional < U > & rhs ) ;
(4) (C++17부터)
template < class T, class U >
constexpr bool operator > ( const optional < T > & lhs, const optional < U > & rhs ) ;
(5) (C++17부터)
template < class T, class U >
constexpr bool operator >= ( const optional < T > & lhs, const optional < U > & rhs ) ;
(6) (C++17부터)
template < class T, std:: three_way_comparable_with < T > U >

constexpr std:: compare_three_way_result_t < T, U >

operator <=> ( const optional < T > & lhs, const optional < U > & rhs ) ;
(7) (C++20 이후)
optional 객체와 nullopt 를 비교합니다
template < class T >
constexpr bool operator == ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(8) (C++17 이후)
template < class T >
constexpr bool operator == ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(9) (C++17부터)
(C++20 이전까지)
template < class T >
constexpr bool operator ! = ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(10) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator ! = ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(11) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator < ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(12) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator < ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(13) (C++17부터)
(C++20 이전까지)
template < class T >
constexpr bool operator <= ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(14) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator <= ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(15) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator > ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(16) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator > ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(17) (C++17부터)
(C++20 이전까지)
template < class T >
constexpr bool operator >= ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(18) (C++17부터)
(C++20까지)
template < class T >
constexpr bool operator >= ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(19) (C++17부터)
(C++20 이전까지)
template < class T >

constexpr std:: strong_ordering

operator <=> ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(20) (C++20 이후)
optional 객체와 값을 비교합니다
template < class T, class U >
constexpr bool operator == ( const optional < T > & opt, const U & value ) ;
(21) (C++17부터)
template < class U, class T >
constexpr bool operator == ( const U & value, const optional < T > & opt ) ;
(22) (C++17 이후)
template < class T, class U >
constexpr bool operator ! = ( const optional < T > & opt, const U & value ) ;
(23) (C++17 이후)
template < class U, class T >
constexpr bool operator ! = ( const U & value, const optional < T > & opt ) ;
(24) (C++17부터)
template < class T, class U >
constexpr bool operator < ( const optional < T > & opt, const U & value ) ;
(25) (C++17부터)
template < class U, class T >
constexpr bool operator < ( const U & value, const optional < T > & opt ) ;
(26) (C++17부터)
template < class T, class U >
constexpr bool operator <= ( const optional < T > & opt, const U & value ) ;
(27) (C++17부터)
template < class U, class T >
constexpr bool operator <= ( const U & value, const optional < T > & opt ) ;
(28) (C++17부터)
template < class T, class U >
constexpr bool operator > ( const optional < T > & opt, const U & value ) ;
(29) (C++17 이후)
template < class U, class T >
constexpr bool operator > ( const U & value, const optional < T > & opt ) ;
(30) (C++17부터)
template < class T, class U >
constexpr bool operator >= ( const optional < T > & opt, const U & value ) ;
(31) (C++17부터)
template < class U, class T >
constexpr bool operator >= ( const U & value, const optional < T > & opt ) ;
(32) (C++17부터)
template < class T, std:: three_way_comparable_with < T > U >

constexpr std:: compare_three_way_result_t < T, U >

operator <=> ( const optional < T > & opt, const U & value ) ;
(33) (C++20 이후)

optional 객체에 대한 비교 연산을 수행합니다.

1-7) 두 개의 optional 객체, lhs rhs 를 비교합니다. 포함된 값은 lhs rhs 모두 값이 포함된 경우에만 비교됩니다( T 의 해당 연산자 사용). 그렇지 않은 경우,
  • lhs lhs rhs 모두 값이 포함되지 않은 경우에만 rhs 같음 으로 간주됩니다.
  • lhs rhs 가 값을 포함하고 lhs 가 값을 포함하지 않는 경우에만 rhs 보다 작음 으로 간주됩니다.
1-6) 다음 각 함수에 대해 @ 를 해당 비교 연산자로 나타냅니다:

해당 표현식 * lhs @ * rhs 이 잘못된 형식이거나 그 결과가 bool 로 변환 가능하지 않으면, 프로그램은 잘못된 형식입니다.

(C++26까지)

이 오버로드는 해당 표현식 * lhs @ * rhs 이 올바른 형식이고 그 결과가 bool 로 변환 가능할 때만 오버로드 해결에 참여합니다.

(C++26부터)
8-20) opt nullopt 와 비교합니다. 값을 포함하지 않는 optional 과 비교할 때 (1-6) 과 동등합니다.

< , <= , > , >= , 그리고 != 연산자들은 각각 operator <=> operator == 로부터 합성됩니다 .

(since C++20)
21-33) opt value 와 비교합니다. 값은 opt 가 값을 포함하는 경우에만 ( T 의 해당 연산자를 사용하여) 비교됩니다. 그렇지 않은 경우, opt value 보다 작은 것으로 간주됩니다.
21-32) 다음 함수들 각각에 대해 @ 를 해당 비교 연산자로 나타내면:

해당 표현식 * opt @ value 또는 value @ * opt (피연산자들의 위치에 따라)이 형성되지 않거나 그 결과가 bool 로 변환 가능하지 않으면, 프로그램은 형성되지 않는다.

(C++26까지)

이 오버로드는 다음 모든 조건이 만족될 때만 오버로드 해결에 참여한다:

  • U std::optional 의 특수화가 아닌 경우.
  • 해당 표현식 * opt @ value 또는 value @ * opt (피연산자들의 위치에 따라)이 형성되고 그 결과가 bool 로 변환 가능한 경우.
(C++26부터)

목차

매개변수

lhs, rhs, opt - 비교할 optional 객체
value - 포함된 값과 비교할 값

반환값

1) lhs. has_value ( ) ! = rhs. has_value ( ) ? false :
( lhs. has_value ( ) == false ? true : * lhs == * rhs )
2) lhs. has_value ( ) ! = rhs. has_value ( ) ? true :
( lhs. has_value ( ) == false ? false : * lhs ! = * rhs )
3) ! rhs ? false : ( ! lhs ? true : * lhs < * rhs )
4) ! lhs ? true : ( ! rhs ? false : * lhs <= * rhs )
5) ! lhs ? false : ( ! rhs ? true : * lhs > * rhs )
6) ! rhs ? true : ( ! lhs ? false : * lhs >= * rhs )
7) lhs && rhs ? * lhs <=> * rhs : lhs. has_value ( ) <=> rhs. has_value ( )
8,9) ! opt
10,11) opt. has_value ( )
12) false
13) opt. has_value ( )
14) ! opt
15) true
16) opt. has_value ( )
17) false
18) true
19) ! opt
20) opt. has_value ( ) <=> false
21) opt. has_value ( ) ? * opt == value : false
22) opt. has_value ( ) ? value == * opt : false
23) opt. has_value ( ) ? * opt ! = value : true
24) opt. has_value ( ) ? value ! = * opt : true
25) opt. has_value ( ) ? * opt < value : true
26) opt. has_value ( ) ? value < * opt : false
27) opt. has_value ( ) ? * opt <= value : true
28) opt. has_value ( ) ? value <= * opt : false
29) opt. has_value ( ) ? * opt > value : false
30) opt. has_value ( ) ? value > * opt : true
31) opt. has_value ( ) ? * opt >= value : false
32) opt. has_value ( ) ? value >= * opt : true
33) opt. has_value ( ) ? * opt <=> value : std :: strong_ordering :: less

예외

1-7) 구현 정의 예외를 발생시킬 수 있습니다.
21-33) 비교가 예외를 발생시킬 때와 무엇을 발생시키는지 설명합니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_constrained_equality 202403L (C++26) 제약된 비교 연산자 for std::optional

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2945 C++17 T-비교 케이스에서 템플릿 매개변수 순서가 일관되지 않음 일관성 있게 수정됨