compare_compare_weak_order_fallback
compare_compare_weak_order_fallback (약한 순서 비교 폴백)
이 페이지는 C++20 표준 라이브러리의 std::compare_weak_order_fallback 사용자 지정 포인트 객체에 대해 다뤄요. 이 객체는 operator<=>를 사용할 수 없는 타입에서도 ==와 < 연산자를 활용해 세 방향 비교를 수행하고 std::weak_ordering 결과를 만들어 줘요. 따라서 기존의 비교 연산자만으로도 약한 순서 비교를 일관되게 사용할 수 있어요.
출처: cppreference
본문
개요
<compare> 헤더에 다음과 같이 정의되어 있어요.
| Defined in header |
||
|---|---|---|
| inline namespace /* unspecified / { inline constexpr / unspecified / compare_weak_order_fallback = / unspecified */ ; } | (since C++20) | |
| Call signature | ||
| template < class T , class U > requires /* see below / constexpr std :: weak_ordering compare_weak_order_fallback ( T && t , U && u ) noexcept ( / see below */ ); | (since C++20) |
이 함수는 부분 표현식 t와 u에 대해 세 방향 비교를 수행하고 std::weak_ordering 타입의 결과를 생성해요. operator<=>를 사용할 수 없는 경우에도 동작해요.
std::decay_t<T>와 std::decay_t<U>가 같은 타입이라면, std::compare_weak_order_fallback(t, u)는 다음 표현식과 동등해요.
std::weak_order(t, u)가 잘 구성된 표현식이라면 그 표현식과 동등해요.- 그렇지 않으면,
t == u와t < u가 모두 잘 구성된 표현식이고decltype(t == u)와decltype(t < u)가 각각boolean-testable모델을 만족할 때,t == u ? std::weak_ordering::equivalent : t < u ? std::weak_ordering::less : std::weak_ordering::greater와 동등해요. 이때t와u는 한 번만 평가돼요.
그 외의 모든 경우에 std::compare_weak_order_fallback(t, u)는 잘못된 형식(ill-formed)이에요. 템플릿 인스턴스화의 즉시 문맥에서 나타나면 치환 실패로 이어질 수 있어요.
사용자 지정 포인트 객체
std::compare_weak_order_fallback이라는 이름은 사용자 지정 포인트 객체를 나타내요. 이 객체는 리터럴 세미레귤러 클래스 타입의 const 함수 객체예요. 자세한 내용은 CustomizationPointObject를 참고해요.
예제
다음 예제는 operator<=>를 지원하지 않는 타입에서도 폴백 메커니즘이 동작하는 모습을 보여줘요.
#include <compare>
#include <iostream>
// does not support <=>
struct Rational_1
{
int num;
int den; // > 0
};
inline constexpr bool operator<(Rational_1 lhs, Rational_1 rhs)
{
return lhs.num * rhs.den < rhs.num * lhs.den;
}
inline constexpr bool operator==(Rational_1 lhs, Rational_1 rhs)
{
return lhs.num * rhs.den == rhs.num * lhs.den;
}
// supports <=>
struct Rational_2
{
int num;
int den; // > 0
};
inline constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs)
{
return lhs.num * rhs.den <=> rhs.num * lhs.den;
}
inline constexpr bool operator==(Rational_2 lhs, Rational_2 rhs)
{
return lhs <=> rhs == 0;
}
void print(int id, std::weak_ordering value)
{
std::cout << id << ") ";
if (value == 0)
std::cout << "equal\n";
else if (value < 0)
std::cout << "less\n";
else
std::cout << "greater\n";
}
int main()
{
Rational_1 a{1, 2}, b{3, 4};
// print(0, a <=> b); // does not work
print(1, std::compare_weak_order_fallback(a, b)); // works, defaults to < and ==
Rational_2 c{6, 5}, d{8, 7};
print(2, c <=> d); // works
print(3, std::compare_weak_order_fallback(c, d)); // works
Rational_2 e{2, 3}, f{4, 6};
print(4, e <=> f); // works
print(5, std::compare_weak_order_fallback(e, f)); // works
}
출력은 다음과 같아요.
1) less
2) greater
3) greater
4) equal
5) equal
결함 보고
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2114 ( P2167R3 ) | C++20 | the fallback mechanism only required return types to be convertible to bool | constraints strengthened |
같이 보기
| weak_order (C++20) | performs 3-way comparison and produces a result of type std::weak_ordering (customization point object) [edit] |
|---|