Qt::totally_ordered_wrapper Class
template <typename P> class Qt::totally_ordered_wrapperQt::totally_ordered_wrapper 포장된 유형에 대해 엄격한 전체 순서를 제공하는 래퍼 유형입니다. 더 보기...
| 헤더: | #include <QtCompare> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 다음부터: | Qt 6.8 |
공개 함수
상세 설명
Qt::totally_ordered_wrapper<P>는 템플릿 클래스로, P 는 래핑할 유형을 지정합니다.
이 클래스의 주요 사용 사례 중 하나는 포인터를 비교할 때 정의되지 않은 동작 (UB)을 방지하는 것입니다.
다음과 같은 간단한 클래스를 생각해 보겠습니다.
template <typename T>
struct PointerWrapperBad {
int val;
T *ptr;
};PointerWrapperBad 타입의 두 인스턴스를 사전 순으로 비교하면, ptr 멤버에 대해 operator<() 또는 operator<=>() 가 호출되므로 UB가 발생합니다.
이 문제를 해결하려면 새로운 래퍼 타입을 사용하십시오:
template <typename T>
struct PointerWrapperGood {
int val;
Qt::totally_ordered_wrapper<T *> ptr;
friend bool
operator==(PointerWrapperGood lhs, PointerWrapperGood rhs) noexcept = default;
friend auto
operator<=>(PointerWrapperGood lhs, PointerWrapperGood rhs) noexecpt = default;
};Qt::totally_ordered_wrapper 유형에 대한 operator<() 및 (사용 가능한 경우) operator<=>() 연산자는 각각 std::less 및 std::compare_three_way 함수 객체를 사용하여, 비교 시 포인터에 대해 엄격한 전체 순서를 제공합니다.
그 결과, PointerWrapperGood::ptr 멤버에 대한 관계 연산자는 명확하게 정의되며, 위에서 보여준 것처럼 PointerWrapperGood 클래스의 관계 연산자에 대해 =default 를 적용할 수도 있습니다.
멤버 함수 문서
[noexcept default] totally_ordered_wrapper::totally_ordered_wrapper()
totally_ordered_wrapper 의 인스턴스를 기본 생성합니다.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.