Namespaces
Variants

std:: compare_weak_order_fallback

From cppreference.net
Utilities library
헤더 파일에 정의됨 <compare>
inline namespace /* unspecified */ {

inline constexpr /* unspecified */
compare_weak_order_fallback = /* unspecified */ ;

}
(C++20부터)
호출 시그니처
template < class T, class U >

requires /* see below */
constexpr std:: weak_ordering

compare_weak_order_fallback ( T && t, U && u ) noexcept ( /* see below */ ) ;
(C++20부터)

부분식 t u 에 대해 삼중 비교를 수행하고, <=> 연산자를 사용할 수 없는 경우에도 std::weak_ordering 타입의 결과를 생성합니다.

만약 std:: decay_t < T > std:: decay_t < U > 가 동일한 타입이라면, std :: compare_weak_order_fallback ( t, u ) 표현식 동등 합니다:

  • std:: weak_order ( t, u ) , 이 표현식이 올바른 형식인 경우; 그렇지 않으면,
  • t == u ? std :: weak_ordering :: equivalent :
    t < u ? std :: weak_ordering :: less :
    std :: weak_ordering :: greater
    , 만약 t == u t < u 표현식이 모두 올바른 형식이고 decltype ( t == u ) decltype ( t < u ) 각각이 boolean-testable 개념을 만족하는 경우, 단 t u 는 한 번만 평가된다.

다른 모든 경우에는, std :: compare_weak_order_fallback ( t, u ) 는 형식이 올바르지 않으며, 이는 템플릿 인스턴스화의 직접적인 맥락에서 나타날 때 치환 실패 를 초래할 수 있습니다.

목차

커스터마이제이션 포인트 객체

이름 std::compare_weak_order_fallback 사용자 정의 지점 객체 를 나타내며, 이는 리터럴 함수 객체 의 const 리터럴 semiregular 클래스 타입입니다. 자세한 내용은 CustomizationPointObject 를 참조하십시오.

예제

#include <compare>
#include <iostream>
// <=>를 지원하지 않음
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;
}
// <=>를 지원함
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); // 작동하지 않음
    print(1, std::compare_weak_order_fallback(a, b)); // 작동함, <와 ==로 기본 설정됨
    Rational_2 c{6, 5}, d{8, 7};
    print(2, c <=> d); // 작동함
    print(3, std::compare_weak_order_fallback(c, d)); // 작동함
    Rational_2 e{2, 3}, f{4, 6};
    print(4, e <=> f); // 작동함
    print(5, std::compare_weak_order_fallback(e, f)); // 작동함
}

출력:

1) less
2) greater
3) greater
4) equal
5) equal

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 2114
( P2167R3 )
C++20 폴백 메커니즘이 반환 타입이
bool 로 변환 가능하기만을 요구함
제약 조건이 강화됨

참고 항목

(C++20)
3방향 비교를 수행하고 std::weak_ordering 타입의 결과를 생성함
(커스터마이제이션 포인트 객체)