Namespaces
Variants

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

From cppreference.net
Utilities library
헤더에 정의됨 <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) 두 개의 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 가 절대로 throw되지 않는다는 점이 다릅니다.
만약 I < sizeof... ( Types ) false 라면, 프로그램은 형식이 잘못되었습니다.
만약 I == v. index ( ) false 라면, 동작은 정의되지 않습니다.

목차

매개변수

lhs,rhs - 비교할 변형들

반환값

연산자 두 피연산자 모두 값을 포함하는 경우
(let I be lhs. index ( ) and J be rhs. index ( ) )
lhs 또는 rhs 가 값이 없는 경우
(let lhs_empty be lhs. valueless_by_exception ( ) and rhs_empty be 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 객체 비교
(함수 템플릿)