operator==, !=, <, <=, >, >=, <=> (std::variant)
|
헤더에 정의됨
<variant>
|
||
|
template
<
class
...
Types
>
constexpr
bool
operator
==
(
const
std::
variant
<
Types...
>
&
lhs,
|
(1) | (C++17부터) |
|
template
<
class
...
Types
>
constexpr
bool
operator
!
=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(2) | (C++17 이후) |
|
template
<
class
...
Types
>
constexpr
bool
operator
<
(
const
std::
variant
<
Types...
>
&
lhs,
|
(3) | (C++17부터) |
|
template
<
class
...
Types
>
constexpr
bool
operator
>
(
const
std::
variant
<
Types...
>
&
lhs,
|
(4) | (C++17 이후) |
|
template
<
class
...
Types
>
constexpr
bool
operator
<=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(5) | (C++17부터) |
|
template
<
class
...
Types
>
constexpr
bool
operator
>=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(6) | (C++17부터) |
|
template
<
class
...
Types
>
constexpr
std::
common_comparison_category_t
|
(7) | (C++20 이후) |
|
헬퍼 함수 템플릿
|
||
|
template
<
std::
size_t
I,
class
...
Types
>
constexpr
const
std::
variant_alternative_t
<
I,
std::
variant
<
Types...
>>
&
|
(8) | ( 설명 전용* ) |
std::variant 객체에 대한 비교 연산을 수행합니다.
T
의 해당 연산자 사용). 그렇지 않은 경우,
- lhs 는 rhs 와 동일한 것으로 간주됩니다. 이는 lhs 와 rhs 가 모두 값을 포함하지 않는 경우에만 해당됩니다.
- lhs 는 rhs 보다 작은 것으로 간주됩니다. 이는 rhs 가 값을 포함하고 lhs 는 값을 포함하지 않거나, lhs. index ( ) 가 rhs. index ( ) 보다 작은 경우에만 해당됩니다.
|
만약 일부
I
값에 대해 해당 표현식
|
(C++26까지) |
|
이 오버로드는 모든
I
값에 대해 해당 표현식
|
(C++26부터) |
GET
는
std::get
(std::variant)
와 유사하게 동작하지만,
std::bad_variant_access
가 절대로 throw되지 않는다는 점이 다릅니다.
목차 |
매개변수
| 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 <=> :
- lhs 만 valueless 상태인 경우 std::strong_ordering::less 를 반환합니다.
- rhs 만 valueless 상태인 경우 std::strong_ordering::greater 를 반환합니다.
- lhs 와 rhs 모두 valueless 상태인 경우 std::strong_ordering::equal 를 반환합니다.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__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
객체 비교
(함수 템플릿) |