future_category — std::future_category

future_category — std::future_category

std::future_categoryfuture와 promise 관련 오류를 위한 정적 오류 범주(error category) 객체에 대한 참조를 얻는 함수예요. C++11에서 도입됐어요. <future> 헤더에 있어요.

error_category::name()이 문자열 "future"를 반환하도록 요구돼요.

출처: cppreference

본문

// <future> 헤더, C++11
const std::error_category& future_category() noexcept;

future·promise·shared_state 관련 오류를 다루는 오류 범주 객체의 참조를 반환해요. std::future_errc의 오류 코드가 이 범주에 속해요.

사용 예

#include <future>
#include <system_error>
#include <iostream>

try {
    std::promise<int> p;
    // ...
} catch (const std::future_error& e) {
    // future_category에서 온 코드
    std::cout << e.code().category().name() << '\n';  // "future"
    std::cout << e.what() << '\n';
}
// error_condition을 만들 때 범주 사용
std::error_condition ec(std::future_errc::no_state, std::future_category());

특징

  • std::future_errc의 열거자(broken_promise, future_already_retrieved 등)를 std::error_code로 다룰 때 연결돼요.
  • std::future_error 예외의 .code()가 이 범주를 가리켜요.
  • 이름(name())은 "future"가 돼요.
std::error_code ec = std::make_error_code(std::future_errc::no_state);
bool is_future = ec.category() == std::future_category();  // true

std::future_category는 비동기 스레드 오류(future/promise)를 표준 오류 코드 시스템에 연결하는 역할을 해요.

더 알아보기 (Learn more)

cppreference