Qt::totally_ordered_wrapper Class
template <typename P> class Qt::totally_ordered_wrapperQt::totally_ordered_wrapper これは、ラップされた型に対して厳密な全順序を提供するラッパー型です。詳細...
| ヘッダー: | #include <QtCompare> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 以下のように: | Qt 6.8 |
パブリック関数
詳細な説明
Qt::totally_ordered_wrapper<P> は、P がラップする型を指定するテンプレートクラスです。
その主な用途の一つは、ポインタを比較する際に未定義の挙動(UB)を防ぐことです。
次の単純なクラスを考えてみましょう。
template <typename T>
struct PointerWrapperBad {
int val;
T *ptr;
};PointerWrapperBad 型の2つのインスタンスを辞書順で比較すると、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 を適用することが可能になります。
メンバ関数のドキュメント
[noexcept default] totally_ordered_wrapper::totally_ordered_wrapper()
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.