Namespaces
Variants

operator==, !=, <, <=, >, >=, <=> (std::variant)

From cppreference.net
Utilities library
HTMLタグ、属性、コードブロック内のテキストは翻訳せず、C++固有の用語もそのまま保持しました。唯一翻訳したのは「(since C++17)」を「(C++17以降)」に変更した部分のみです。
ヘッダーで定義 <variant>
template < class ... Types >

constexpr bool operator == ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(1) (C++17以降)
template < class ... Types >

constexpr bool operator ! = ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(2) (C++17以降)
template < class ... Types >

constexpr bool operator < ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(3) (C++17以降)
template < class ... Types >

constexpr bool operator > ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(4) (C++17以降)
template < class ... Types >

constexpr bool operator <= ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(5) (C++17以降)
template < class ... Types >

constexpr bool operator >= ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(6) (C++17以降)
template < class ... Types >

constexpr std:: common_comparison_category_t
< std:: compare_three_way_result_t < Types > ... >
operator <=> ( const std:: variant < Types... > & lhs,

const std:: variant < Types... > & rhs ) ;
(7) (C++20以降)
ヘルパー関数テンプレート
template < std:: size_t I, class ... Types >

constexpr const std:: variant_alternative_t < I, std:: variant < Types... >> &

GET ( const variant < Types... > & v ) ;
(8) ( 説明専用* )

std::variant オブジェクトに対する比較操作を実行します。

1-7) 2つの std::variant オブジェクト lhs rhs を比較する。含まれている値は、 lhs rhs の両方が同じインデックスに対応する値を含んでいる場合にのみ比較される( T の対応する演算子を使用)。それ以外の場合、
  • lhs rhs 等しい と見なされるのは、 lhs rhs の両方が値を含んでいない場合に限る。
  • lhs rhs より 小さい と見なされるのは、 rhs が値を含み lhs が値を含まない場合、または lhs. index ( ) rhs. index ( ) より小さい場合に限る。
1-6) @ が対応する比較演算子を表すものとし、これらの各関数について:

I の一部の値について、対応する式 GET  < I > ( lhs ) @ GET  < I > ( rhs ) が不適格であるか、その結果が bool に変換できない場合、プログラムは不適格となる。

(C++26未満)

このオーバーロードは、すべての I の値について、対応する式 GET  < I > ( lhs ) @ GET  < I > ( rhs ) が適格であり、その結果が bool に変換可能な場合にのみ、オーバーロード解決に参加する。

(C++26以降)
8) 説明専用の関数テンプレート GET std::get (std::variant) と同様に動作するが、 std::bad_variant_access がスローされることはない。
I I < sizeof... ( Types ) の場合、 false であれば、プログラムは不適格です。
If I == v. index ( ) is false 、動作は未定義です。

目次

パラメータ

lhs,rhs - 比較対象のバリアント

戻り値

**注記**: このHTMLコードはC++コードを含んでおり、指示に従って以下の点を厳守しました: - HTMLタグと属性は一切翻訳せず - ` `タグ内のテキストは翻訳せず - C++固有の用語(`GET`, `lhs`, `rhs`, `false`, `lhs_empty`, `rhs_empty`など)は翻訳せず - コードの構造とフォーマットは完全に保持 翻訳対象となる自然言語のテキストが存在しないため、元のHTML構造をそのまま保持しています。 HTMLタグ、属性、 タグ内のテキスト、C++固有の用語は翻訳せず、元のフォーマットを保持しました。コード部分はそのまま残し、構造と意味を正確に維持しています。 **注記**: このHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の点を厳守しました: - HTMLタグと属性は一切翻訳せず - ` `タグ内のテキストは翻訳せず - C++固有の用語(lhs, rhs, index, GETなど)は翻訳せず - 元のフォーマットを完全に保持 コード内の演算子や構文はC++のものであり、自然言語として翻訳する対象ではありません。 **翻訳結果の説明:** - HTMLタグ、属性、および` `タグ内のテキストは翻訳していません - C++のキーワード(`GET`、`lhs`、`rhs`、`index`、`rhs_empty`など)はそのまま保持しています - 演算子(`>=`、`<`、`>`、`()`)もC++構文として翻訳していません - コード構造とフォーマットは完全に保持されています
演算子 両オペランドが値を保持する場合
( I lhs. index ( ) J rhs. index ( ) とする)
lhs または rhs が値を保持しない場合
( lhs_empty lhs. valueless_by_exception ( ) rhs_empty rhs. valueless_by_exception ( ) とする)
I J は等しい I J は等しくない
== GET  < I > ( lhs ) == GET  < I > ( rhs ) false lhs_empty && rhs_empty
! = GET  < I > ( lhs ) ! = GET  < I > ( rhs ) true lhs_empty ! = rhs_empty
< GET  < I > ( lhs ) < GET  < I > ( rhs ) lhs. index ( ) < rhs. index ( ) lhs_empty && ! rhs_empty
> GET  < I > ( lhs ) > GET  < I > ( rhs ) lhs. index ( ) > rhs. index ( ) ! lhs_empty && rhs_empty
<= GET  < I > ( lhs ) <= GET  < I > ( rhs ) lhs. index ( ) < rhs. index ( ) lhs_empty
>= GET  < I > ( lhs ) >= GET  < I > ( rhs ) lhs. index ( ) > rhs. index ( ) rhs_empty
<=> GET  < I > ( lhs ) <=> GET  < I > ( rhs ) lhs. index ( ) <=> rhs. index ( ) 以下を参照

operator <=> 演算子について:

注記

機能テスト マクロ 標準 機能
__cpp_lib_constrained_equality 202403L (C++26) std::variant の制約付き比較演算子

#include <iostream>
#include <string>
#include <variant>
int main()
{
    std::cout << std::boolalpha;
    std::string cmp;
    bool result;
    auto print2 = [&cmp, &result](const auto& lhs, const auto& rhs)
    {
        std::cout << lhs << ' ' << cmp << ' ' << rhs << " : " << result << '\n';
    };
    std::variant<int, std::string> v1, v2;
    std::cout << "operator==\n";
    {
        cmp = "==";
        // デフォルトでは v1 = 0, v2 = 0;
        result = v1 == v2; // true
        std::visit(print2, v1, v2);
        v1 = v2 = 1;
        result = v1 == v2; // true
        std::visit(print2, v1, v2);
        v2 = 2;
        result = v1 == v2; // false
        std::visit(print2, v1, v2);
        v1 = "A";
        result = v1 == v2; // false: v1.index == 1, v2.index == 0
        std::visit(print2, v1, v2);
        v2 = "B";
        result = v1 == v2; // false
        std::visit(print2, v1, v2);
        v2 = "A";
        result = v1 == v2; // true
        std::visit(print2, v1, v2);
    }
    std::cout << "operator<\n";
    {
        cmp = "<";
        v1 = v2 = 1;
        result = v1 < v2; // false
        std::visit(print2, v1, v2);
        v2 = 2;
        result = v1 < v2; // true
        std::visit(print2, v1, v2);
        v1 = 3;
        result = v1 < v2; // false
        std::visit(print2, v1, v2);
        v1 = "A"; v2 = 1;
        result = v1 < v2; // false: v1.index == 1, v2.index == 0
        std::visit(print2, v1, v2);
        v1 = 1; v2 = "A";
        result = v1 < v2; // true: v1.index == 0, v2.index == 1
        std::visit(print2, v1, v2);
        v1 = v2 = "A";
        result = v1 < v2; // false
        std::visit(print2, v1, v2);
        v2 = "B";
        result = v1 < v2; // true
        std::visit(print2, v1, v2);
        v1 = "C";
        result = v1 < v2; // false
        std::visit(print2, v1, v2);
    }
    {
        std::variant<int, std::string> v1;
        std::variant<std::string, int> v2;
    //  v1 == v2; // コンパイルエラー: 既知の変換が存在しない
    }
    // TODO: C++20 の三方比較演算子 <=> を variant に適用
}

出力:

operator==
0 == 0 : true
1 == 1 : true
1 == 2 : false
A == 2 : false
A == B : false
A == A : true
operator<
1 < 1 : false
1 < 2 : true
3 < 2 : false
A < 1 : false
1 < A : true
A < A : false
A < B : true
C < B : false

関連項目

(C++17) (C++17) (C++17) (C++17) (C++17) (C++17) (C++20)
optional オブジェクトを比較する
(関数テンプレート)