Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
Qt::totally_ordered_wrapper< P > Class Template Reference

\inmodule QtCore \inheaderfile QtCompare More...

#include <qcomparehelpers.h>

+ Collaboration diagram for Qt::totally_ordered_wrapper< P >:

Public Member Functions

 totally_ordered_wrapper () noexcept=default
 
Q_IMPLICIT constexpr totally_ordered_wrapper (std::nullptr_t)
 
constexpr totally_ordered_wrapper (P p) noexcept
 
constexpr P get () const noexcept
 
constexpr void reset (P p) noexcept
 
constexpr P operator-> () const noexcept
 
constexpr T & operator* () const noexcept
 
constexpr operator bool () const noexcept
 

Friends

void qt_ptr_swap (totally_ordered_wrapper &lhs, totally_ordered_wrapper &rhs) noexcept
 
void swap (totally_ordered_wrapper &lhs, totally_ordered_wrapper &rhs) noexcept
 
size_t qHash (totally_ordered_wrapper key, size_t seed=0) noexcept
 

Detailed Description

template<typename P>
class Qt::totally_ordered_wrapper< P >

\inmodule QtCore \inheaderfile QtCompare

Qt::totally_ordered_wrapper is a wrapper type that provides strict total order for the wrapped types.

Since
6.8

One of its primary usecases is to prevent {Undefined Behavior} (UB) when comparing pointers.

Consider the following simple class:

template <typename T>
struct PointerWrapperBad {
int val;
T *ptr;
};
static ControlElement< T > * ptr(QWidget *widget)
GLuint GLfloat * val

Lexicographical comparison of the two instances of the PointerWrapperBad type would result in UB, because it will call {operator<()} or {operator<=>()} on the {ptr} members.

To fix it, use the new wrapper type:

template <typename T>
struct PointerWrapperGood {
int val;
friend bool
operator==(PointerWrapperGood lhs, PointerWrapperGood rhs) noexcept = default;
friend auto
operator<=>(PointerWrapperGood lhs, PointerWrapperGood rhs) noexecpt = default;
};
\inmodule QtCore \inheaderfile QtCompare
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220

The {operator<()} and (if available) {operator<=>()} operators for the {Qt::totally_ordered_wrapper} type use the \l {https://en.cppreference.com/w/cpp/utility/functional/less}{std::less} and \l {https://en.cppreference.com/w/cpp/utility/compare/compare_three_way} {std::compare_three_way} function objects respectively, providing \l {https://en.cppreference.com/w/cpp/language/operator_comparison#Pointer_total_order} {strict total order over pointers} when doing the comparison.

As a result, the relational operators for {PointerWrapperGood::ptr} member will be well-defined, and we can even {=default} the relational operators for the {PointerWrapperGood} class, like it's shown above.

Definition at line 643 of file qcomparehelpers.h.

Constructor & Destructor Documentation

◆ totally_ordered_wrapper() [1/3]

template<typename P >
Qt::totally_ordered_wrapper< P >::totally_ordered_wrapper ( )
defaultnoexcept

◆ totally_ordered_wrapper() [2/3]

template<typename P >
Q_IMPLICIT constexpr Qt::totally_ordered_wrapper< P >::totally_ordered_wrapper ( std::nullptr_t )
inlineconstexpr

Definition at line 651 of file qcomparehelpers.h.

◆ totally_ordered_wrapper() [3/3]

template<typename P >
constexpr Qt::totally_ordered_wrapper< P >::totally_ordered_wrapper ( P p)
inlineexplicitconstexprnoexcept

Definition at line 654 of file qcomparehelpers.h.

Member Function Documentation

◆ get()

template<typename P >
constexpr P Qt::totally_ordered_wrapper< P >::get ( ) const
inlineconstexprnoexcept

Definition at line 656 of file qcomparehelpers.h.

Referenced by Qt::totally_ordered_wrapper< P >::operator bool(), Qt::totally_ordered_wrapper< P >::operator*(), and Qt::totally_ordered_wrapper< P >::operator->().

+ Here is the caller graph for this function:

◆ operator bool()

template<typename P >
constexpr Qt::totally_ordered_wrapper< P >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Definition at line 661 of file qcomparehelpers.h.

References Qt::totally_ordered_wrapper< P >::get().

+ Here is the call graph for this function:

◆ operator*()

template<typename P >
constexpr T & Qt::totally_ordered_wrapper< P >::operator* ( ) const
inlineconstexprnoexcept

Definition at line 659 of file qcomparehelpers.h.

References Qt::totally_ordered_wrapper< P >::get().

+ Here is the call graph for this function:

◆ operator->()

template<typename P >
constexpr P Qt::totally_ordered_wrapper< P >::operator-> ( ) const
inlineconstexprnoexcept

Definition at line 658 of file qcomparehelpers.h.

References Qt::totally_ordered_wrapper< P >::get().

+ Here is the call graph for this function:

◆ reset()

template<typename P >
constexpr void Qt::totally_ordered_wrapper< P >::reset ( P p)
inlineconstexprnoexcept

Definition at line 657 of file qcomparehelpers.h.

Friends And Related Symbol Documentation

◆ qHash

template<typename P >
size_t qHash ( totally_ordered_wrapper< P > key,
size_t seed = 0 )
friend

Definition at line 703 of file qcomparehelpers.h.

◆ qt_ptr_swap

template<typename P >
void qt_ptr_swap ( totally_ordered_wrapper< P > & lhs,
totally_ordered_wrapper< P > & rhs )
friend

Definition at line 699 of file qcomparehelpers.h.

◆ swap

template<typename P >
void swap ( totally_ordered_wrapper< P > & lhs,
totally_ordered_wrapper< P > & rhs )
friend

Definition at line 701 of file qcomparehelpers.h.


The documentation for this class was generated from the following file: