Namespaces
Variants

std:: compare_three_way

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
헤더에 정의됨 <compare>
헤더에 정의됨 <functional>
struct compare_three_way ;
(C++20부터)

비교 연산을 수행하는 함수 객체입니다. 함수 호출 연산자의 매개변수 타입과 반환 타입을 추론합니다.

목차

중첩 타입

중첩 타입 정의
is_transparent unspecified

멤버 함수

operator()
두 인자에 대한 삼중 비교 결과를 얻음
(public member function)

std::compare_three_way:: operator()

template < class T, class U >
constexpr auto operator ( ) ( T && t, U && u ) const ;

다음 표현식 std:: forward < T > ( t ) <=> std:: forward < U > ( u ) expr 로 가정할 때:

  • 만약 T 에서 P 로의 변환 시퀀스나 U 에서 P 로의 변환 시퀀스가 동등성 보존 을 만족하지 않으면, 동작은 정의되지 않습니다.
  • 그렇지 않은 경우:

이 오버로드는 std:: three_way_comparable_with < T, U > 가 만족될 때만 오버로드 해결에 참여합니다.

예제

#include <compare>
#include <iostream>
struct Rational
{
    int num;
    int den; // > 0
    // Although the comparison X <=> Y will work, a direct call
    // to std::compare_three_way{}(X, Y) requires the operator==
    // be defined, to satisfy the std::three_way_comparable_with.
    constexpr bool operator==(Rational const&) const = default;
};
constexpr std::weak_ordering operator<=>(Rational lhs, Rational rhs)
{
    return lhs.num * rhs.den <=> rhs.num * lhs.den;
}
void print(std::weak_ordering value)
{
    value < 0 ? std::cout << "less\n" :
    value > 0 ? std::cout << "greater\n" :
                std::cout << "equal\n";
}
int main()
{
    Rational a{6, 5};
    Rational b{8, 7};
    print(a <=> b);
    print(std::compare_three_way{}(a, b));
}

출력:

greater
greater

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3530 C++20 포인터 비교 시 구문적 검사가 완화됨 의미론적 요구사항만 완화됨

참고 항목

제한된 함수 객체 구현: x == y
(클래스)
제한된 함수 객체 구현: x ! = y
(클래스)
제한된 함수 객체 구현: x < y
(클래스)
제한된 함수 객체 구현: x > y
(클래스)
제한된 함수 객체 구현: x <= y
(클래스)
제한된 함수 객체 구현: x >= y
(클래스)