compare_weak_ordering (약한 순서 비교)
std::weak_ordering은 C++20에서 도입된 삼중 비교(three-way comparison)의 결과 타입이에요. 이 타입은 여섯 가지 관계 연산자를 모두 지원하면서도, 동등한 값이 반드시 구별 불가능하지는 않은 약한 순서(weak ordering)를 나타내요. 또한 비교 불가능한 값은 허용하지 않아서, 두 값 사이에는 항상 a < b, a == b, a > b 중 정확히 하나만 성립해요.
출처: cppreference
본문
정의
| 헤더에 정의됨 |
|
|
| class weak_ordering ; |
|
(C++20부터) |
std::weak_ordering 클래스 타입은 다음과 같은 특성을 가진 삼중 비교의 결과 타입이에요.
- 여섯 가지 관계 연산자(==, !=, <, <=, >, >=)를 모두 허용해요.
- 대체 가능성(substitutability)을 의미하지 않아요. 즉, a가 b와 동등하더라도 f(a)가 f(b)와 동등하지 않을 수 있어요. 여기서 f는 인자의 public const 멤버를 통해 접근 가능한 비교에 중요한 상태만 읽는 함수를 의미해요. 다시 말해, 동등한 값이라도 서로 구별될 수 있어요.
- 비교 불가능한 값은 허용하지 않아요. a < b, a == b, a > b 중 정확히 하나만 참이어야 해요.
상수
std::weak_ordering 타입은 세 가지 유효한 값을 가지며, 해당 타입의 const static 데이터 멤버로 구현돼 있어요.
| 이름 |
정의 |
| less [static] |
작음(앞에 정렬됨) 관계를 나타내는 유효한 값 (public static 멤버 상수) |
| equivalent [static] |
동등함(앞이나 뒤에 정렬되지 않음)을 나타내는 유효한 값 (public static 멤버 상수) |
| greater [static] |
큼(뒤에 정렬됨) 관계를 나타내는 유효한 값 (public static 멤버 상수) |
변환
std::weak_ordering은 std::partial_ordering으로 암시적으로 변환 가능해요. 반면 std::strong_ordering은 weak_ordering으로 암시적으로 변환 가능해요.
| operator partial_ordering |
std::partial_ordering으로의 암시적 변환 (public 멤버 함수) |
std::weak_ordering::operator partial_ordering
| constexpr operator partial_ordering () const noexcept ; |
|
|
반환값
v가 less이면 std::partial_ordering::less, v가 greater이면 std::partial_ordering::greater, v가 equivalent이면 std::partial_ordering::equivalent를 반환해요.
비교 연산
이 타입의 값과 리터럴 0 사이에 비교 연산자가 정의돼 있어요. 이는 a <=> b == 0 또는 a <=> b < 0 같은 표현식을 지원해서, 삼중 비교 연산자의 결과를 불리언 관계로 변환할 수 있게 해줘요. std::is_eq, std::is_lt 등을 참고하세요.
이 함수들은 일반적인 비한정(unqualified) 또는 한정(qualified) 조회로는 보이지 않으며, std::weak_ordering이 인수의 연관 클래스(associated class)일 때 인자 의존 조회(argument-dependent lookup)로만 찾을 수 있어요.
weak_ordering을 정수 리터럴 0 이외의 다른 것과 비교하려는 프로그램의 동작은 정의되지 않아요.
| operator== operator< operator> operator<= operator>= operator<=> |
0 또는 weak_ordering과 비교 (함수) |
operator==
| friend constexpr bool operator == ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr bool operator == ( weak_ordering v , weak_ordering w ) noexcept = default ; |
(2) |
|
매개변수
| v, w |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
operator<
| friend constexpr bool operator < ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr bool operator < ( /unspecified/ u , weak_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
operator<=
| friend constexpr bool operator <= ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr bool operator <= ( /unspecified/ u , weak_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
operator>
| friend constexpr bool operator > ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr bool operator > ( /unspecified/ u , weak_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
operator>=
| friend constexpr bool operator >= ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr bool operator >= ( /unspecified/ u , weak_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
operator<=>
| friend constexpr weak_ordering operator <=> ( weak_ordering v , /unspecified/ u ) noexcept ; |
(1) |
|
| friend constexpr weak_ordering operator <=> ( /unspecified/ u , weak_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::weak_ordering 값 |
| u |
- |
리터럴 0 인수를 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
예제
같이 보기
| strong_ordering (C++20) |
6개 연산자를 모두 지원하고 대체 가능한 3방향 비교의 결과 타입 (클래스) [편집] |
| partial_ordering (C++20) |
6개 연산자를 모두 지원하고, 대체 가능하지 않으며, 비교 불가능한 값을 허용하는 3방향 비교의 결과 타입 (클래스) [편집] |
더 알아보기 (Learn more)
cppreference