totally_ordered_wrapper Class
template <typename P> class Qt::totally_ordered_wrapperQt::totally_ordered_wrapper 는 래퍼 유형으로 래핑된 유형에 대해 엄격한 총 순서를 제공합니다. 더 보기...
Header: | #include <QtCompare> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
이후: | Qt 6.8 |
자세한 설명
주요 사용 사례 중 하나는 포인터를 비교할 때 정의되지 않은 동작 (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
으로 정의할 수도 있습니다.
© 2025 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.