std:: compare_three_way
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<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 로 가정할 때:
-
-
두 변환된 포인터(타입
P)를 구현 정의된 포인터의 엄격한 전체 순서 에 따라 비교합니다:
-
- 만약 t 가 u 보다 앞서면, std::strong_ordering::less 를 반환합니다.
- 만약 u 가 t 보다 앞서면, std::strong_ordering::greater 를 반환합니다.
- 그렇지 않으면, std::strong_ordering::equal 를 반환합니다.
-
만약
T에서P로의 변환 시퀀스나U에서P로의 변환 시퀀스가 동등성 보존 을 만족하지 않으면, 동작은 정의되지 않습니다.
-
두 변환된 포인터(타입
- 그렇지 않은 경우:
-
- expr 의 결과를 반환합니다.
- 만약 std:: three_way_comparable_with < T, U > 가 만족되지 않으면, 동작은 정의되지 않습니다.
이 오버로드는 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 | 포인터 비교 시 구문적 검사가 완화됨 | 의미론적 요구사항만 완화됨 |
참고 항목
|
(C++20)
|
제한된 함수 객체 구현:
x
==
y
(클래스) |
|
(C++20)
|
제한된 함수 객체 구현:
x
!
=
y
(클래스) |
|
(C++20)
|
제한된 함수 객체 구현:
x
<
y
(클래스) |
|
(C++20)
|
제한된 함수 객체 구현:
x
>
y
(클래스) |
|
(C++20)
|
제한된 함수 객체 구현:
x
<=
y
(클래스) |
|
(C++20)
|
제한된 함수 객체 구현:
x
>=
y
(클래스) |