compare_strong_ordering (강한 순서 비교)
이 페이지는 C++20에서 도입된 std::strong_ordering 클래스 타입에 대해 설명해요. std::strong_ordering은 삼방향 비교(three-way comparison)의 결과 타입으로, 모든 관계 연산자를 지원하고 치환 가능성을 보장하며 비교 불가능한 값이 없어요. 이 타입은 <compare> 헤더에 정의돼 있어요.
출처: cppreference
본문
<compare> 헤더에 정의됨 |
|
|
class strong_ordering ; |
|
(C++20부터) |
std::strong_ordering 클래스 타입은 다음 조건을 만족하는 삼방향 비교의 결과 타입이에요.
- 여섯 가지 관계 연산자(
==, !=, <, <=, >, >=)를 모두 지원해요.
- 치환 가능성을 의미해요:
a가 b와 동등하면 f(a)도 f(b)와 동등해요. 여기서 f는 인자의 public const 멤버를 통해 접근 가능한 비교에 중요한 상태만 읽는 함수를 뜻해요. 즉, 동등한 값은 서로 구별할 수 없어요.
- 비교 불가능한 값을 허용하지 않아요:
a < b, a == b, a > b 중 정확히 하나만 참이어야 해요.
상수
std::strong_ordering 타입은 네 가지 유효한 값을 가지며, 이 값들은 타입의 const 정적 데이터 멤버로 구현돼 있어요.
| 이름 |
정의 |
less [static] |
작음(ordered before) 관계를 나타내는 유효한 값 (public static 멤버 상수) |
equivalent [static] |
동등함(ordered before도 ordered after도 아님)을 나타내는 유효한 값. equal과 동일함 (public static 멤버 상수) |
equal [static] |
동등함(ordered before도 ordered after도 아님)을 나타내는 유효한 값. equivalent와 동일함 (public static 멤버 상수) |
greater [static] |
큼(ordered after) 관계를 나타내는 유효한 값 (public static 멤버 상수) |
변환
std::strong_ordering은 세 가지 비교 범주 중 가장 강력한 타입이에요. 다른 어떤 범주에서도 암시적으로 변환되지 않으며, 나머지 두 범주로는 암시적으로 변환돼요.
operator partial_ordering |
std::partial_ordering로의 암시적 변환 (public 멤버 함수) |
std::strong_ordering:: operator partial_ordering
constexpr operator partial_ordering () const noexcept ; |
|
|
반환값
v가 less이면 std::partial_ordering::less, v가 greater이면 std::partial_ordering::greater, v가 equal 또는 equivalent이면 std::partial_ordering::equivalent를 반환해요.
operator weak_ordering |
std::weak_ordering로의 암시적 변환 (public 멤버 함수) |
std::strong_ordering:: operator weak_ordering
constexpr operator weak_ordering () const noexcept ; |
|
|
반환값
v가 less이면 std::weak_ordering::less, v가 greater이면 std::weak_ordering::greater, v가 equal 또는 equivalent이면 std::weak_ordering::equivalent를 반환해요.
비교
이 타입의 값과 리터럴 0 사이에 비교 연산자가 정의돼 있어요. 이를 통해 a <=> b == 0 또는 a <=> b < 0 같은 표현식을 사용해 삼방향 비교 연산자의 결과를 불리언 관계로 변환할 수 있어요. std::is_eq, std::is_lt 등을 참고하세요.
이 함수들은 일반적인 비한정(unqualified) 또는 한정(qualified) 조회로는 보이지 않으며, std::strong_ordering이 인자의 연관 클래스(associated class)일 때 인자 의존 조회(argument-dependent lookup)로만 찾을 수 있어요.
strong_ordering을 정수 리터럴 0이 아닌 다른 것과 비교하려는 프로그램의 동작은 정의되지 않아요.
operator== operator< operator> operator<= operator>= operator<=> |
0 또는 strong_ordering과 비교 (함수) |
operator==
friend constexpr bool operator == ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr bool operator == ( strong_ordering v , strong_ordering w ) noexcept = default ; |
(2) |
|
매개변수
| v, w |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 true 또는 false를 반환해요.
operator<
friend constexpr bool operator < ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr bool operator < ( /*unspecified*/ u , strong_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 true 또는 false를 반환해요.
operator<=
friend constexpr bool operator <= ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr bool operator <= ( /*unspecified*/ u , strong_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 true 또는 false를 반환해요.
operator>
friend constexpr bool operator > ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr bool operator > ( /*unspecified*/ u , strong_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 true 또는 false를 반환해요.
operator>=
friend constexpr bool operator >= ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr bool operator >= ( /*unspecified*/ u , strong_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 true 또는 false를 반환해요.
operator<=>
friend constexpr strong_ordering operator <=> ( strong_ordering v , /*unspecified*/ u ) noexcept ; |
(1) |
|
friend constexpr strong_ordering operator <=> ( /*unspecified*/ u , strong_ordering v ) noexcept ; |
(2) |
|
매개변수
| v |
- |
확인할 std::strong_ordering 값 |
| u |
- |
리터럴 0을 받아들이는 모든 타입의 사용되지 않는 매개변수 |
반환값
비교 결과에 따라 std::strong_ordering 값을 반환해요.
예제
#include <compare>
#include <iostream>
struct Point
{
int x{}, y{};
friend constexpr std::strong_ordering operator<=>(Point lhs, Point rhs)
{
if (lhs.x < rhs.x or (lhs.x == rhs.x and lhs.y < rhs.y))
return std::strong_ordering::less;
if (lhs.x > rhs.x or (lhs.x == rhs.x and lhs.y > rhs.y))
return std::strong_ordering::greater;
return std::strong_ordering::equivalent;
}
friend std::ostream& operator<<(std::ostream& os, Point s)
{
return os << '(' << s.x << ',' << s.y << ')';
}
};
void print_three_way_comparison(const auto& p, const auto& q)
{
const auto cmp{p <=> q};
std::cout << p
<< (cmp < 0 ? " < " : cmp > 0 ? " > " : " == " ) // compares with 0
<< q << '\n';
}
void print_two_way_comparison(const auto& p, const auto& q)
{
std::cout << p
<< (p < q ? " < " : p > q ? " > " : " == ") // compares p and q
<< q << '\n';
}
int main()
{
const Point p1{0, 1}, p2{0, 1}, p3{0, 2};
print_three_way_comparison(p1, p2);
print_two_way_comparison(p1, p2);
print_three_way_comparison(p2, p3);
print_two_way_comparison(p2, p3);
print_three_way_comparison(p3, p2);
print_two_way_comparison(p3, p2);
}
출력:
(0,1) == (0,1)
(0,1) == (0,1)
(0,1) < (0,2)
(0,1) < (0,2)
(0,2) > (0,1)
(0,2) > (0,1)
같이 보기
weak_ordering (C++20) |
6개 연산자를 모두 지원하고 치환 가능하지 않은 3-way 비교의 결과 타입 (클래스) [edit] |
partial_ordering (C++20) |
6개 연산자를 모두 지원하고 치환 가능하지 않으며 비교 불가능한 값을 허용하는 3-way 비교의 결과 타입 (클래스) [edit] |
더 알아보기 (Learn more)
cppreference