<QtTypeTraits> - Qt Type Traits

型特性と変換のための機能。詳細...

Header: #include <QtTypeTraits>
Since: Qt 6.5

関数

T qExchange(T &obj, U &&newValue)
(since 6.2) std::underlying_type_t<Enum> qToUnderlying(Enum e)

マクロ

(since 6.8) QT_NO_QASCONST
(since 6.6) QT_NO_QEXCHANGE

詳細説明

関数のドキュメント

[constexpr noexcept(...)] template <typename T, typename U = T> T qExchange(T &obj, U &&newValue)

obj の値をnewValue に置き換え、古い値obj を返します。

これは Qt による std::exchange() の実装です。std::exchange()との違いは、C++20以前では既にconstexpr 、C++23以前では既にnoexceptとなっている点のみです。

C++20 や C++23 の亜種を必要としない場合は、std::exchange() を使用することを強くお勧めします。QT_NO_QEXCHANGE マクロを定義することで、qExchange() を使用できないようにすることができます。

以下は、qExchange() を使用して移動コンストラクタを実装する方法です:

MyClass(MyClass &&other)
  : m_pointer{qExchange(other.m_pointer, nullptr)},
    m_int{qExchange(other.m_int, 0)},
    m_vector{std::move(other.m_vector)},
    ...

クラス型のメンバについては、std::move()を使用することができる。しかし、未加工のポインタや整数型のようなスカラー型では、moveはcopyと同じであり、特にポインタでは期待されたものではありません。しかし、std::exchange()/qExchange()を使えば、コンストラクタが例外で終了した場合に便利なように、次のデータ・メンバの初期化までにソース・オブジェクトのメンバがすでにリセットされていることを確認できる。

以下は、qExchange()を使用して、反復処理するコレクションを消費するループを記述する方法です:

for (auto &e : qExchange(collection, {})
    doSomethingWith(e);

これは次のコードと同じで、もっと冗長なコードです:

{
    auto tmp = std::move(collection);
    collection = {};                    // or collection.clear()
    for (auto &e : tmp)
        doSomethingWith(e);
}                                       // destroys 'tmp'

これは完全に安全である。forループはqExchange()の結果を、ループが実行されている間、一時変数の宣言を保存して生かしておくからである。ただし、qExchange() は nononst オブジェクトを返すので、Qt コンテナが切り離される可能性があることに注意してください。

注意: std::conjunction_v<std::is_nothrow_move_constructible<T>, std::is_nothrow_assignable<T &, U>>true の場合、この関数は noexcept となります。

[constexpr noexcept, since 6.2] template <typename Enum> std::underlying_type_t<Enum> qToUnderlying(Enum e)

列挙子e を、その列挙子の基底型で表現される同等の値に変換します。

この関数は Qt 6.2 で導入されました。

マクロのドキュメント

[since 6.8] QT_NO_QASCONST

このマクロを定義すると、qAsConst()関数を使用できなくなります。

このマクロは Qt 6.8 で導入されました。

qAsConstも参照してください

[since 6.6] QT_NO_QEXCHANGE

このマクロを定義すると、qExchange() 関数が使用できなくなります。

このマクロは Qt 6.6 で導入されました。

qExchangeも参照してください

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