optional_operator_cmp

optional_operator_cmp (optional 비교 연산자)

이 페이지에서는 std::optional 객체를 다른 optional 객체, nullopt, 또는 일반 값과 비교하는 연산자들을 다뤄요. C++17부터 제공되며, C++20에서 삼자 비교 연산자(<=>)가 추가되었어요.

출처: cppreference

본문

개요

<optional> 헤더에 정의된 비교 연산자들은 다음과 같아요.

<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 객체에 대한 비교 연산은 다음과 같이 동작해요.

  • lhs와 rhs 모두 값을 포함하지 않을 때에만 lhs가 rhs와 같다고 간주해요.
  • rhs가 값을 포함하고 lhs가 값을 포함하지 않을 때에만 lhs가 rhs보다 작다고 간주해요.
해당 표현식 *lhs @ *rhs가 ill-formed이거나 그 결과가 bool로 변환 가능하지 않으면 프로그램은 ill-formed이에요. (C++26까지)
이 오버로드는 해당 표현식 *lhs @ *rhs가 well-formed이고 그 결과가 bool로 변환 가능할 때만 오버로드 해석에 참여해요. (C++26부터)
<, <=, >, >=, != 연산자는 각각 operator<=>와 operator==로부터 합성돼요. (C++20부터)
해당 표현식 *opt @ value 또는 value @ *opt (피연산자의 위치에 따라)가 ill-formed이거나 그 결과가 bool로 변환 가능하지 않으면 프로그램은 ill-formed이에요. (C++26까지)
이 오버로드는 다음 조건이 모두 충족될 때만 오버로드 해석에 참여해요: U가 std::optional의 특수화가 아니고, 해당 표현식 *opt @ value 또는 value @ *opt (피연산자의 위치에 따라)가 well-formed이며 그 결과가 bool로 변환 가능해야 해요. (C++26부터)

매개변수

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

반환값

명시된 내용이 없어요.

예외

명시된 내용이 없어요.

참고

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

결함 보고

이전에 발표된 C++ 표준에 소급 적용된 동작 변경 결함 보고서는 다음과 같아요.

DR 적용 대상 공개된 동작 올바른 동작
LWG 2945 C++17 compare-with-T 경우에 템플릿 매개변수 순서가 일관되지 않음 일관되게 수정됨

더 알아보기 (Learn more)

cppreference