std::hash

std::hash (std::error_code 특수화)

std::hashstd::error_code 특수화예요. std::error_code 객체의 해시 값을 얻을 수 있게 해줘요. 해시 기반 컨테이너에 오류 코드를 넣을 수 있게 해줘요. C++11부터 있어요.

출처: cppreference

본문

<system_error> 헤더에 정의돼 있고, std::hashstd::error_code 특수화예요.

template<>
struct hash<std::error_code>;

이 특수화는 std::error_code 타입 객체의 해시를 계산해요. 해시는 오류 코드의 값(value())과 카테고리를 함께 고려해 계산되므로, 동등 비교(==)에서 같은 코드는 같은 해시를 가져요.

덕분에 std::error_codestd::unordered_set, std::unordered_map 같은 해시 기반 컨테이너의 키로 사용할 수 있어요. 예를 들어 오류 코드별 처리 함수를 매핑하는 테이블을 만들 때 유용해요.

#include <system_error>
#include <unordered_map>
#include <string>

std::unordered_map<std::error_code, std::string> handlers;
handlers[std::errc::no_such_file_or_directory] = "파일 없음 처리";

더 알아보기 (Learn more)

cppreference