std::error_condition

std::error_condition (오류 조건)

오류 코드와 독립적으로, 오류의 "의미적 조건"을 나타내는 타입이에요. 포팅 가능한 오류 조건을 서로 다른 오류 코드 시스템 간에 비교하기 위해 쓰여요. C++11부터 있어요.

출처: cppreference

본문

<system_error> 헤더에 정의돼 있고, 오류 조건을 나타내는 타입이에요.

class error_condition;

std::error_condition은 오류 코드의 구체적인 숫자 값이 아니라, 그 오류가 나타내는 "조건(condition)"을 나타내요. std::error_code와 마찬가지로 정수 값과 std::error_category 포인터를 보관하지만, 용도가 달라요 — 서로 다른 오류 코드 시스템(예: POSIX vs Win32)이 나타내는 동일한 오류 조건을 하나의 error_condition으로 통합해 비교할 수 있게 해요.

멤버 함수:

  • (생성자) 기본, error_condition(int, const error_category&), Enum→ErrorConditionEnum 변환 등.
  • value(): 오류 조건의 정수 값을 돌려줘요.
  • category(): 오류 조건의 카테고리.
  • message(): 사람이 읽을 수 있는 설명.
  • operator bool: 조건이 0이 아닌지 확인.
  • operator==/operator!=: 두 조건(또는 코드와 조건)을 비교.

전형적인 사용: std::error_codedefault_error_condition()이 돌려주는 error_conditionstd::errc 값이 만든 조건을 비교해서, 플랫폼이 달라도 같은 오류인지 판단해요.

#include <system_error>
#include <iostream>

std::error_condition filename_too_long = std::errc::filename_too_long;
// ec.code()가 긴 파일 이름 오류일 때
if (ec == filename_too_long)
    std::cout << "파일 이름이 너무 길어요\n";

std::error_conditionmake_error_condition·std::errc와 함께 쓰여, 오류 코드의 포팅 가능한 의미 비교를 가능하게 해요.

더 알아보기 (Learn more)

cppreference