Qt::totally_ordered_wrapper Class
template <typename P> class Qt::totally_ordered_wrapperQt::totally_ordered_wrapper es un tipo envoltorio que proporciona un orden total estricto para los tipos envueltos. Más información...
| Encabezado: | #include <QtCompare> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| Desde: | Qt 6.8 |
Funciones públicas
Descripción detallada
Qt::totally_ordered_wrapper<P> es una clase de plantilla en la que P especifica el tipo que se va a envolver.
Uno de sus principales usos es evitar el comportamiento indefinido (UB) al comparar punteros.
Consideremos la siguiente clase sencilla:
template <typename T>
struct PointerWrapperBad {
int val;
T *ptr;
};La comparación lexicográfica de las dos instancias del tipo ` PointerWrapperBad ` daría lugar a un comportamiento indefinido (UB), ya que se llamarían a ` operator<() ` o ` operator<=>() ` sobre los miembros ` ptr `.
Para solucionarlo, utiliza el nuevo tipo envoltorio:
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;
};Los operadores operator<() y (si está disponible) operator<=>() para el tipo Qt::totally_ordered_wrapper utilizan los objetos de función std::less y std::compare_three_way, respectivamente, lo que proporciona un orden total estricto sobre los punteros al realizar la comparación.
Como resultado, los operadores relacionales del miembro ` PointerWrapperGood::ptr ` estarán bien definidos, e incluso podremos definir los operadores relacionales de la clase ` PointerWrapperGood ` mediante ` =default `, tal y como se muestra más arriba.
Documentación de las funciones miembro
[noexcept default] totally_ordered_wrapper::totally_ordered_wrapper()
Crea por defecto una instancia 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.