Nesta página

Qt::totally_ordered_wrapper Class

template <typename P> class Qt::totally_ordered_wrapper

Qt::totally_ordered_wrapper é um tipo de envoltório que fornece uma ordem total estrita para os tipos envolvidos. Mais...

Cabeçalho: #include <QtCompare>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Desde: Qt 6.8

Funções públicas

Descrição detalhada

Qt::totally_ordered_wrapper<P> é uma classe de modelo em que P especifica o tipo a ser encapsulado.

Um de seus principais casos de uso é evitar Comportamento Indefinido (UB) ao comparar ponteiros.

Considere a seguinte classe simples:

template <typename T>
struct PointerWrapperBad {
    int val;
    T *ptr;
};

A comparação lexicográfica das duas instâncias do tipo ` PointerWrapperBad ` resultaria em comportamento indefinido (UB), pois chamaria ` operator<() ` ou ` operator<=>() ` nos membros ` ptr `.

Para corrigir isso, use o novo tipo wrapper:

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;
};

Os operadores operator<() e (se disponível) operator<=>() para o tipo Qt::totally_ordered_wrapper utilizam os objetos de função std::less e std::compare_three_way, respectivamente, garantindo uma ordem total estrita entre ponteiros ao realizar a comparação.

Como resultado, os operadores relacionais para o membro ` PointerWrapperGood::ptr ` serão bem-definidos, e podemos até mesmo ` =default ` os operadores relacionais para a classe ` PointerWrapperGood `, como mostrado acima.

Documentação da função membro

[noexcept default] totally_ordered_wrapper::totally_ordered_wrapper()

Cria, por padrão, uma instância de ` 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.