tuple_operator_cmp (tuple 비교 연산자)
이 페이지에서는 std::tuple에 대한 비교 연산자들을 다뤄요. 이 연산자들은 두 튜플을 사전식(lexicographically)으로 비교하며, C++20부터는 우주선 연산자(<=>)도 제공돼요. 또한 C++23부터는 tuple-like 타입과의 비교도 지원해요.
출처: cppreference
본문
오버로드
Defined in header <tuple> |
|
|
template < class ... TTypes , class ... UTypes > bool operator == ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(1) |
(since C++11) (constexpr since C++14) |
template < class ... TTypes , class ... UTypes > bool operator != ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(2) |
(since C++11) (constexpr since C++14) (until C++20) |
template < class ... TTypes , class ... UTypes > bool operator < ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(3) |
(since C++11) (constexpr since C++14) (until C++20) |
template < class ... TTypes , class ... UTypes > bool operator <= ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(4) |
(since C++11) (constexpr since C++14) (until C++20) |
template < class ... TTypes , class ... UTypes > bool operator > ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(5) |
(since C++11) (constexpr since C++14) (until C++20) |
template < class ... TTypes , class ... UTypes > bool operator >= ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(6) |
(since C++11) (constexpr since C++14) (until C++20) |
template < class ... TTypes , class ... UTypes > constexpr std :: common_comparison_category_t < synth - three - way - result < TTypes , Elems > ... > operator <=> ( const std :: tuple < TTypes ... >& lhs , const std :: tuple < UTypes ... >& rhs ); |
(7) |
(since C++20) |
template < class ... TTypes , tuple - like UTuple > constexpr bool operator == ( const tuple < TTypes ... >& lhs , const UTuple & rhs ); |
(8) |
(since C++23) |
template < class ... TTypes , tuple - like UTuple > constexpr std :: common_comparison_category_t < synth - three - way - result < TTypes , /* Elems */ > ... > operator <=> ( const tuple < TTypes ... >& lhs , const UTuple & rhs ); |
(9) |
(since C++23) |
제약 조건
sizeof...(TTypes)가 sizeof...(UTypes)와 같지 않거나, [0, sizeof...(Types)) 범위의 어떤 i에 대해서도 std::get<i>(lhs) == std::get<i>(rhs)가 유효한 표현식이 아니라면, 프로그램은 ill-formed예요. [0, sizeof...(Types)) 범위의 어떤 i에 대해서도 std::get<i>(lhs) == std::get<i>(rhs)의 타입과 값 범주가 BooleanTestable 요구 사항을 충족하지 않으면, 동작은 정의되지 않아요. |
(until C++26) |
이 오버로드는 sizeof...(TTypes)가 sizeof...(UTypes)와 같고, 모든 i에 대해 std::get<i>(lhs) == std::get<i>(rhs)가 유효한 표현식이며 decltype(std::get<i>(lhs) == std::get<i>(rhs))가 boolean-testable 모델을 충족할 때만 오버로드 해석에 참여해요. |
(since C++26) |
동작
- 빈 튜플에 대해서는
std::strong_ordering::equal을 반환해요.
- 비어 있지 않은 튜플의 경우, 다음 코드와 동등한 효과를 가져요:
if ( auto c = synth-three-way ( std :: get < 0 > ( lhs ), std :: get < 0 > ( rhs )); c != 0 ) return c ; if ( auto c = synth-three-way ( std :: get < 1 > ( lhs ), std :: get < 1 > ( rhs )); c != 0 ) return c ; ... return synth-three-way ( std :: get < N - 1 > ( lhs ), std :: get < N - 1 > ( rhs ));
모든 비교 연산자는 단락 평가(short-circuit)돼요. 비교 결과를 결정하는 데 필요한 요소만 접근하고, 그 이상은 접근하지 않아요.
<, <=, >, >=, != 연산자는 각각 operator<=>와 operator==로부터 합성돼요. |
(since C++20) |
매개변수
반환값
각 연산자의 정의에 따라 결정돼요.
참고
관계 연산자는 각 요소의 operator<를 기준으로 정의돼요. |
(until C++20) |
관계 연산자는 synth-three-way를 기준으로 정의돼요. synth-three-way는 가능하면 operator<=>를 사용하고, 그렇지 않으면 operator<를 사용해요. 특히 요소 타입 자체에 operator<=>가 없지만 삼방향 비교 가능한 타입으로 암시적으로 변환될 수 있다면, operator< 대신 그 변환이 사용돼요. |
(since C++20) |
| Feature-test macro |
Value |
Std |
Feature |
__cpp_lib_constrained_equality |
202403L |
(C++26) |
std::tuple에 대한 제약된 operator== |
예제
operator<가 튜플에 대해 정의되어 있으므로, 튜플의 컨테이너를 정렬할 수 있어요.
#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
int main()
{
std::vector<std::tuple<int, std::string, float>> v
{
{2, "baz", -0.1},
{2, "bar", 3.14},
{1, "foo", 10.1},
{2, "baz", -1.1},
};
std::sort(v.begin(), v.end());
for (const auto& p: v)
std::cout << "{ " << get<0>(p)
<< ", " << get<1>(p)
<< ", " << get<2>(p)
<< " }\n";
}
출력:
{ 1, foo, 10.1 }
{ 2, bar, 3.14 }
{ 2, baz, -1.1 }
{ 2, baz, -0.1 }
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR |
Applied to |
Behavior as published |
Correct behavior |
| LWG 2114 ( P2167R3 ) |
C++11 |
boolean 연산에 대한 타입 전제 조건이 없었음 |
추가됨 |
같이 보기
operator== operator!= operator< operator<= operator> operator>= operator<=> (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20) |
pair의 값을 사전식으로 비교한다 (함수 템플릿) [edit] |
더 알아보기 (Learn more)
cppreference