In questa pagina

Qt::totally_ordered_wrapper Class

template <typename P> class Qt::totally_ordered_wrapper

Qt::totally_ordered_wrapper è un tipo wrapper che garantisce un ordine totale rigoroso per i tipi avvolti. Per saperne di più...

Intestazione: #include <QtCompare>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
A partire da: Qt 6.8

Funzioni pubbliche

Descrizione dettagliata

Qt::totally_ordered_wrapper<P> è una classe modello in cui P specifica il tipo da incapsulare.

Uno dei suoi principali casi d'uso è quello di prevenire il comportamento indefinito (UB) durante il confronto dei puntatori.

Si consideri la seguente semplice classe:

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

Il confronto lessicografico delle due istanze del tipo ` PointerWrapperBad ` comporterebbe un comportamento indefinito (UB), poiché verrebbero chiamati i metodi ` operator<() ` o ` operator<=>() ` sui membri ` ptr `.

Per risolvere il problema, utilizzare il nuovo 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;
};

Gli operatori operator<() e (se disponibile) operator<=>() per il tipo Qt::totally_ordered_wrapper utilizzano rispettivamente gli oggetti funzione std::less e std::compare_three_way, garantendo un ordine totale stretto sui puntatori durante il confronto.

Di conseguenza, gli operatori relazionali per il membro ` PointerWrapperGood::ptr ` saranno ben definiti e potremo persino ` =default ` gli operatori relazionali per la classe ` PointerWrapperGood `, come mostrato sopra.

Documentazione delle funzioni membro

[noexcept default] totally_ordered_wrapper::totally_ordered_wrapper()

Crea un'istanza di ` totally_ordered_wrapper` con i valori predefiniti.

© 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.