operator==,!=,<,<=,>,>=,<=>(std::tuple)
| ヘッダ <tuple>
|
||
template< class... TTypes, class... UTypes >
bool operator==( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
で定義 (1) | (C++11 以上) (C++14 より constexpr) |
template< class... TTypes, class... UTypes >
bool operator!=( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(2) | (C++11 以上) (C++14 より constexpr) (C++20 まで) |
template< class... TTypes, class... UTypes >
bool operator<( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(3) | (C++11 以上) (C++14 より constexpr) (C++20 まで) |
template< class... TTypes, class... UTypes >
bool operator<=( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(4) | (C++11 以上) (C++14 より constexpr) (C++20 まで) |
template< class... TTypes, class... UTypes >
bool operator>( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(5) | (C++11 以上) (C++14 より constexpr) (C++20 まで) |
template< class... TTypes, class... UTypes >
bool operator>=( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(6) | (C++11 以上) (C++14 より constexpr) (C++20 まで) |
template< class... TTypes, class... UTypes >
constexpr std::common_comparison_category_t<
synth-three-way-result<TTypes, Elems>...>
operator<=>( const std::tuple<TTypes...>& lhs,
const std::tuple<UTypes...>& rhs );
|
(7) | (C++20 以上) |
template< class... TTypes, tuple-like UTuple >
constexpr bool operator==( const tuple<TTypes...>& lhs, const UTuple& rhs );
|
(8) | (C++23 以上) |
template< class... TTypes, tuple-like UTuple >
constexpr std::common_comparison_category_t<
synth-three-way-result<TTypes, /* Elems */>...>
operator<=>( const tuple<TTypes...>& lhs, const UTuple& rhs );
|
(9) | (C++23 以上) |
lhs のすべての要素とタプル rhs の対応する要素を operator== で比較します。true を返します。!(lhs == rhs) を返します。
sizeof...(TTypes) が sizeof...(UTypes) と等しくない場合、または std::get<i>(lhs) == std::get<i>(rhs) が i in [0, sizeof...(Types)) のいずれかに対して有効な式でない場合、プログラムは ill-formed です。 |
要件を満たさない場合、動作は未定義です。 |
|
(C++26 まで) sizeof...(TTypes) このオーバーロードは、sizeof...(UTypes) が std::get<i>(lhs) == std::get<i>(rhs) と等しく、decltype(std::get<i>(lhs) == std::get<i>(rhs)) が有効な式であり、 が in i のすべてに対して [0, sizeof...(Types))boolean-testable |
をモデル化する場合にのみオーバーロード解決に参加します。 |
lhs と rhs を operator< により辞書順で比較します。つまり、最初の要素を比較し、それらが同等であれば2番目の要素を比較し、それらが同等であれば3番目の要素を比較し、以下同様です。false を返します。空でないタプルの場合、効果は以下と同等です。if (std::get<0>(lhs) < std::get<0>(rhs)) return true; if (std::get<0>(rhs) < std::get<0>(lhs)) return false; if (std::get<1>(lhs) < std::get<1>(rhs)) return true; if (std::get<1>(rhs) < std::get<1>(lhs)) return false; ... return std::get<N - 1>(lhs) < std::get<N - 1>(rhs);
!(rhs < lhs) を返します。rhs < lhs を返します。!(lhs < rhs) を返します。sizeof...(TTypes) が sizeof...(UTypes) と等しくない場合、または上記の同等文に示された比較式のいずれかが有効な式でない場合、プログラムは ill-formed です。lhs と rhs を synth-three-way により辞書順で比較します。つまり、最初の要素を比較し、それらが同等であれば2番目の要素を比較し、それらが同等であれば3番目の要素を比較し、以下同様です。
- 空のタプルの場合は std::strong_ordering::equal を返します。
- 空でないタプルの場合、効果は以下と同等です。
if (auto c = synth-three-way(std::get<0>(lhs), std::get<0>(rhs)); c != 0) return c;
if (auto c = synth-three-way(std::get<1>(lhs), std::get<1>(rhs)); c != 0) return c;
...
return synth-three-way(std::get<N - 1>(lhs), std::get<N - 1>(rhs));
rhs が tuple-like オブジェクトであり、rhs の要素数が代わりに std::tuple_size_v<UTuple> によって決定される点が異なります。このオーバーロードは argument-dependent lookup によってのみ発見できます。rhs が tuple-like オブジェクトである点が異なります。/* Elems */ は、std::tuple_element_t<i, UTuple> in i の各 [0, std::tuple_size_v<UTuple>) に対して昇順の型 のパックを表します。このオーバーロードは argument-dependent lookupによってのみ発見できます。
|
すべての比較演算子はショートサーキットされます。つまり、比較の結果を決定するために必要な範囲を超えてタプル要素にアクセスすることはありません。
|
(C++20 以上) |
パラメータ
| lhs, rhs | - | 比較対象のタプル |
戻り値
[
0
,
sizeof...
(
Types
)
)
の範囲内の全ての
i
に対して成り立つ場合。それ以外の場合は
false
を返す。二つの空タプルの場合は
true
を返す。
注記
|
関係演算子は各要素の operator < を用いて定義される。 |
(C++20まで) |
|
関係演算子は synth-three-way を用いて定義され、これは可能な場合は operator <=> を、そうでない場合は operator < を使用する。 特に、要素型自体が operator <=> を提供していないが、三方比較可能な型に暗黙変換可能な場合、その変換が operator < の代わりに使用される。 |
(C++20以降) |
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_constrained_equality
|
202403L
|
(C++26) | 制約付き operator == を std::tuple に追加 |
例
operator < がタプルに対して定義されているため、タプルのコンテナをソートすることができます。
#include <algorithm> #include <iostream> #include <tuple> #include <vector> int main() { std::vector<std::tuple<int, std::string, float>> v { {2, "baz", -0.1}, {2, "bar", 3.14}, {1, "foo", 10.1}, {2, "baz", -1.1}, }; std::sort(v.begin(), v.end()); for (const auto& p: v) std::cout << "{ " << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << " }\n"; }
出力:
{ 1, foo, 10.1 }
{ 2, bar, 3.14 }
{ 2, baz, -1.1 }
{ 2, baz, -0.1 }
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
|
LWG 2114
( P2167R3 ) |
C++11 | 論理演算の型事前条件が欠落していた | 追加された |
関連項目
|
(C++20で削除)
(C++20で削除)
(C++20で削除)
(C++20で削除)
(C++20で削除)
(C++20)
|
pair内の値を辞書順に比較する
(関数テンプレート) |