Namespaces
Variants

std:: strong_ordering

From cppreference.net
Utilities library
헤더에 정의됨 <compare>
class strong_ordering ;
(C++20부터)

클래스 타입 std::strong_ordering 는 다음과 같은 3-way 비교 의 결과 타입입니다:

  • 여섯 가지 관계 연산자를 모두 허용합니다 ( == , != , < , <= , > , >= ).
  • 대체 가능성을 의미함: a b 와 동등하다면, f ( a ) 또한 f ( b ) 와 동등함. 여기서 f 는 인자의 public const 멤버를 통해 접근 가능한 비교 관련 상태만 읽는 함수를 나타냄. 즉, 동등한 값들은 구분할 수 없음.
  • 비교 불가능한 값을 허용하지 않음 : a < b , a == b , a > b 중 정확히 하나가 true 여야 함.

목차

상수

타입 std::strong_ordering 은 네 가지 유효한 값을 가지며, 해당 타입의 const static 데이터 멤버로 구현됩니다:

이름 정의
inline constexpr std :: strong_ordering less
[static]
미만(보다 앞서 정렬됨) 관계를 나타내는 유효한 값
(공개 정적 멤버 상수)
inline constexpr std :: strong_ordering equivalent
[static]
동등함(보다 앞서 정렬되지도 않고 뒤에 정렬되지도 않음)을 나타내는 유효한 값, equal 과 동일함
(공개 정적 멤버 상수)
inline constexpr std :: strong_ordering equal
[static]
동등함(보다 앞서 정렬되지도 않고 뒤에 정렬되지도 않음)을 나타내는 유효한 값, equivalent 과 동일함
(공개 정적 멤버 상수)
inline constexpr std :: strong_ordering greater
[static]
초과(보다 뒤에 정렬됨) 관계를 나타내는 유효한 값
(공개 정적 멤버 상수)

변환

std::strong_ordering 는 세 가지 비교 범주 중 가장 강력한 범주입니다: 다른 어떤 범주에서도 암시적으로 변환될 수 없으며, 다른 두 범주로는 암시적으로 변환될 수 있습니다.

operator partial_ordering
std::partial_ordering 으로의 암시적 변환
(공개 멤버 함수)

std::strong_ordering:: operator partial_ordering

constexpr operator partial_ordering ( ) const noexcept ;

반환값

std::partial_ordering::less 만약 v less 인 경우, std::partial_ordering::greater 만약 v greater 인 경우, std::partial_ordering::equivalent 만약 v equal 또는 equivalent 인 경우.

operator weak_ordering
std::weak_ordering으로의 암시적 변환 std::weak_ordering
(public member function)

std::strong_ordering:: operator weak_ordering

constexpr operator weak_ordering ( ) const noexcept ;

반환값

std::weak_ordering::less 만약 v less 인 경우, std::weak_ordering::greater 만약 v greater 인 경우, std::weak_ordering::equivalent 만약 v equal 이거나 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<=>
zero 또는 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 인수를 받아들이는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) true 만약 v equivalent 이거나 equal 인 경우, false 만약 v less 이거나 greater 인 경우
2) true 두 매개변수가 동일한 값을 가지는 경우, false 그렇지 않은 경우. equal equivalent 과 동일함을 참고하십시오.

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 인수를 받아들이는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) true 만약 v less 이면, 그리고 false 만약 v greater , equivalent , 또는 equal 이면
2) true 만약 v greater 이면, 그리고 false 만약 v less , equivalent , 또는 equal 이면

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 인수를 받아들이는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) true 만약 v less , equivalent , 또는 equal 이면, 그리고 false 만약 v greater 이면
2) true 만약 v greater , equivalent , 또는 equal 이면, 그리고 false 만약 v less 이면

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 인수를 허용하는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) true 만약 v greater 이면, 그리고 false 만약 v less , equivalent , 또는 equal 이면
2) true 만약 v less 이면, 그리고 false 만약 v greater , equivalent , 또는 equal 이면

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 인수를 받아들이는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) true 만약 v greater , equivalent , 또는 equal 이면, 그리고 false 만약 v less 이면
2) true 만약 v less , equivalent , 또는 equal 이면, 그리고 false 만약 v greater 이면

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 인수를 받아들이는 임의의 타입의 사용되지 않는 매개변수

반환 값

1) v .
2) greater 만약 v less 인 경우, less 만약 v greater 인 경우, 그 외의 경우에는 v .

예제

#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 ? " >  " : " == " ) // 0과 비교
              << q << '\n';
}
void print_two_way_comparison(const auto& p, const auto& q)
{
    std::cout << p
              << (p < q ? " <  " : p > q ? " >  " : " == ") // p와 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)

참고 항목

6개 연산자를 모두 지원하고 대체 불가능한 3-way 비교의 결과 타입
(class)
6개 연산자를 모두 지원하고, 대체 불가능하며, 비교 불가능한 값을 허용하는 3-way 비교의 결과 타입
(class)