关于std::errc、std::io_errc与std::future_errc错误类型分类差异的设计疑问及error_condition可移植错误代码含义咨询
1. Is the difference in enum specializations intentional?
Absolutely, this is a deliberate design choice tied to the core distinction between std::error_code and std::error_condition in the C++ standard:
std::error_coderepresents concrete, platform or library-specific error values. These are tied to a specific "category" (like system errors, IO library errors, or future library errors) and map directly to underlying implementation details.std::error_conditionrepresents abstract, semantic error conditions that are portable across platforms. They describe what went wrong in a way that's not tied to a specific system or library's error numbering.
Here's how this applies to your examples:
std::errcis specialized foris_error_condition_enumbecause it's meant to represent abstract error semantics (likeerrc::permission_denied,errc::file_not_found) that translate to different system-specific error codes across platforms. It's a common vocabulary for talking about errors regardless of the OS.std::io_errcandstd::future_errcare specialized foris_error_code_enumbecause they're library-specific error codes tied directly to the C++ standard library's IO and future components. These errors aren't meant to map to system-level errors—they're internal to the library's logic, so they don't need the abstract, cross-platform translation thaterror_conditionprovides.
2. What does "portable error code" mean for std::error_condition, if std::errc values differ across platforms?
Great catch—this is a common point of confusion, and the key here is to separate numeric values from semantic meaning. When we say std::error_condition is "portable," we're not talking about the underlying integer values being the same across platforms. We're talking about the semantics being consistent.
For example:
- On Linux,
std::errc::permission_deniedmaps to the POSIXEPERMvalue (usually 1). - On Windows, it might map to a different system error code (like
ERROR_ACCESS_DENIED, which is 5).
But when you write code like this:
if (some_error_condition == std::errc::permission_denied) { // Handle "permission was denied" }
This check works identically across platforms because you're checking the semantic condition, not the raw numeric value. The std::error_condition abstraction handles mapping the platform-specific error_code to the correct abstract condition for you.
In short, "portable" here means you can reason about errors using a common, OS-agnostic vocabulary instead of having to memorize or conditionalize on platform-specific error numbers.
内容的提问来源于stack exchange,提问作者Kingsley Chen

