Namespaces
Variants

operator== (std::expected)

From cppreference.net
Utilities library
기본 템플릿
template < class T2, class E2 >

requires ( ! std:: is_void_v < T2 > )
friend constexpr bool operator == ( const expected & lhs,

const std:: expected < T2, E2 > & rhs ) ;
(1) (C++23부터)
template < class E2 >

friend constexpr bool operator == ( const expected & lhs,

const std:: unexpected < E2 > & unex ) ;
(2) (C++23부터)
template < class T2 >
friend constexpr bool operator == ( const expected & lhs, const T2 & val ) ;
(3) (C++23부터)
void 부분 특수화
template < class T2, class E2 >

requires std:: is_void_v < T2 >
friend constexpr bool operator == ( const expected & lhs,

const std:: expected < T2, E2 > & rhs ) ;
(4) (C++23부터)
template < class E2 >

friend constexpr bool operator == ( const expected & lhs,

const std:: unexpected < E2 > & unex ) ;
(5) (C++23부터)

std::expected 객체에 대한 비교 연산을 수행합니다.

1) 두 개의 std::expected 객체를 비교합니다. 두 객체는 lhs rhs 모두 동일한 expected 값을 포함하거나, 모두 동일한 unexpected 값을 포함하는 경우에만 동일하게 비교됩니다.

다음 표현식 중 어느 하나라도 잘못 formed되거나 그 결과가 bool 로 변환 가능하지 않으면 프로그램은 잘못 formed됩니다:

(C++26까지)

이 오버로드는 다음 모든 표현식이 잘 formed되고 그 결과가 bool 로 변환 가능한 경우에만 오버로드 해결에 참여합니다:

(C++26부터)
  • * lhs == * rhs
  • lhs. error ( ) == rhs. error ( )
2) std::expected 객체와 std::unexpected 객체를 비교합니다. 두 객체는 lhs unex. error ( ) 와 동일한 예상치 못한 값을 포함하는 경우에만 동일하게 비교됩니다.

표현식 lhs. error ( ) == unex. error ( ) 이 형식이 잘못되었거나 그 결과가 bool 로 변환 가능하지 않은 경우, 프로그램은 형식이 잘못됩니다.

(C++26 이전)

이 오버로드는 표현식 lhs. error ( ) == unex. error ( ) 이 형식이 올바르고 그 결과가 bool 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

(C++26 이후)
3) std::expected 객체와 기대값을 비교합니다. 객체들은 다음 조건에서만 동일하게 비교됩니다: lhs val 와 동일한 기대값을 포함하는 경우.

표현식 * lhs == val 이 형성 불가능하거나, 그 결과가 bool 으로 변환 불가능한 경우, 프로그램은 형성 불가능합니다.

(C++26 이전)

이 오버로드는 다음 모든 조건이 충족될 때만 오버로드 해결에 참여합니다:

  • T2 std::expected 의 특수화가 아닌 경우.
  • 표현식 * lhs == val 이 형성 가능하고, 그 결과가 bool 으로 변환 가능한 경우.
(C++26 이후)
4) 두 개의 std::expected 객체를 비교합니다. 두 객체는 lhs rhs 가 모두 기대값을 나타내거나, 둘 다 동일한 예상치 못한 값을 포함하는 경우에만 동일하게 비교됩니다.

표현식 lhs. error ( ) == rhs. error ( ) 이 형성되지 않거나 그 결과가 bool 로 변환 가능하지 않으면 프로그램은 형성되지 않습니다.

(C++26 이전)

이 오버로드는 표현식 lhs. error ( ) == rhs. error ( ) 이 형성되고 그 결과가 bool 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

(C++26 이후)
5) std::expected 객체와 std::unexpected 객체를 비교합니다. 두 객체는 lhs 가 예상치 못한 값을 포함하고 있으며, 그 값이 unex. error ( ) 와 같을 경우에만 동일하게 비교됩니다.

표현식 lhs. error ( ) == unex. error ( ) 이 형식에 맞지 않거나 그 결과가 bool 로 변환 가능하지 않을 경우, 프로그램은 형식에 맞지 않습니다.

(C++26 이전)

이 오버로드는 표현식 lhs. error ( ) == unex. error ( ) 이 형식에 맞고, 그 결과가 bool 로 변환 가능한 경우에만 오버로드 해결에 참여합니다.

(C++26 이후)

이 함수들은 일반적인 unqualified lookup 또는 qualified lookup 으로는 보이지 않으며, 인수가 std::expected<T, E> 를 associated class로 가질 때에만 argument-dependent lookup 을 통해서만 찾을 수 있습니다.

!= 연산자는 합성된 operator== 로부터 생성됩니다.

목차

매개변수

lhs, rhs - std::expected 비교 대상 객체
unex - std::unexpected 와 비교할 값 lhs
val - 가 포함한 기대값과 비교할 값 lhs

반환값

1)
lhs. has_value ( ) ! = rhs. has_value ( ) ? false :
( lhs. has_value ( ) ? * lhs == * rhs : lhs. error ( ) == rhs. error ( ) )
2) ! lhs. has_value ( ) && static_cast < bool > ( lhs. error ( ) == unex. error ( ) )
3) lhs. has_value ( ) && static_cast < bool > ( * lhs == val )

4)
lhs. has_value ( ) ! = rhs. has_value ( ) ? false :
lhs. has_value ( ) || static_cast < bool > ( lhs. error ( ) == rhs. error ( ) )
5) ! lhs. has_value ( ) && static_cast < bool > ( lhs. error ( ) == unex. error ( ) )

예외

비교가 예외를 발생시킬 때와 무엇을 발생시키는지에 대해 설명합니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_constrained_equality 202411L (C++26) std::expected 에 대한 제약된 비교 연산자

예제

#include <expected>
#include <iostream>
#include <string_view>
using namespace std::string_view_literals;
int main()
{
    auto x1{"\N{GREEN HEART}"sv};
    auto x2{"\N{CROSS MARK}"sv};
    std::expected<std::string_view, int> e1{x1}, e2{x1}, e3{x2};
    std::unexpected u1{13};
    std::cout << "Overload (1):\n"
              << e1.value() << (e1 == e2 ? " == " : " != ") << *e2 << '\n'
              << e1.value() << (e1 != e3 ? " != " : " == ") << *e3 << "\n\n";
    std::cout << "Overload (2):\n"
              << e1.value() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n';
    e1 = std::unexpected{13};
    std::cout << e1.error() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n';
    e1 = std::unexpected{31};
    std::cout << e1.error() << (e1 != u1 ? " != " : " == ") << u1.error() << '\n';
    std::cout << "Overload (3):\n"
              << *e1 << (e1 == x1 ? " == " : " != ") << x1 << '\n'
              << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n";
}

출력:

Overload (1):
💚 == 💚
💚 != ❌
Overload (2):
💚 != 13
13 == 13
31 != 13
Overload (3):
💚 == 💚
💚 != ❌

참고 항목

(C++23)
std::unexpected 객체들을 비교합니다
(함수 템플릿)