19 Diagnostics library [diagnostics]

19.5 System error support [syserr]

19.5.4 Class error_code [syserr.errcode]


19.5.4.1 Overview [syserr.errcode.overview]

19.5.4.2 Constructors [syserr.errcode.constructors]

19.5.4.3 Modifiers [syserr.errcode.modifiers]

19.5.4.4 Observers [syserr.errcode.observers]

19.5.4.5 Non-member functions [syserr.errcode.nonmembers]

19.5.4.6 Formatting [syserr.fmt]


19.5.4.1 Overview [syserr.errcode.overview]

The class error_code describes an object used to hold error code values, such as those originating from the operating system or other low-level application program interfaces.
[Note 1: 
Class error_code is an adjunct to error reporting by exception.
— end note]
namespace std { class error_code { public: // [syserr.errcode.constructors], constructors error_code() noexcept; error_code(int val, const error_category& cat) noexcept; template<class ErrorCodeEnum> error_code(ErrorCodeEnum e) noexcept; // [syserr.errcode.modifiers], modifiers void assign(int val, const error_category& cat) noexcept; template<class ErrorCodeEnum> error_code& operator=(ErrorCodeEnum e) noexcept; void clear() noexcept; // [syserr.errcode.observers], observers int value() const noexcept; const error_category& category() const noexcept; error_condition default_error_condition() const noexcept; string message() const; explicit operator bool() const noexcept; private: int val_; // exposition only const error_category* cat_; // exposition only }; // [syserr.errcode.nonmembers], non-member functions error_code make_error_code(errc e) noexcept; template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const error_code& ec); }

19.5.4.2 Constructors [syserr.errcode.constructors]

error_code() noexcept;
Effects: Initializes val_ with 0 and cat_ with &system_category().
error_code(int val, const error_category& cat) noexcept;
Effects: Initializes val_ with val and cat_ with &cat.
template<class ErrorCodeEnum> error_code(ErrorCodeEnum e) noexcept;
Constraints: is_error_code_enum_v<ErrorCodeEnum> is true.
Effects: Equivalent to: error_code ec = make_error_code(e); assign(ec.value(), ec.category());

19.5.4.3 Modifiers [syserr.errcode.modifiers]

void assign(int val, const error_category& cat) noexcept;
Postconditions: val_ == val and cat_ == &cat.
template<class ErrorCodeEnum> error_code& operator=(ErrorCodeEnum e) noexcept;
Constraints: is_error_code_enum_v<ErrorCodeEnum> is true.
Effects: Equivalent to: error_code ec = make_error_code(e); assign(ec.value(), ec.category());
Returns: *this.
void clear() noexcept;
Postconditions: value() == 0 and category() == system_category().

19.5.4.4 Observers [syserr.errcode.observers]

int value() const noexcept;
Returns: val_.
const error_category& category() const noexcept;
Returns: *cat_.
error_condition default_error_condition() const noexcept;
Returns: category().default_error_condition(value()).
string message() const;
Returns: category().message(value()).
explicit operator bool() const noexcept;
Returns: value() != 0.

19.5.4.5 Non-member functions [syserr.errcode.nonmembers]

error_code make_error_code(errc e) noexcept;
Returns: error_code(static_cast<int>(e), generic_category()).
template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const error_code& ec);
Effects: Equivalent to: return os << ec.category().name() << ':' << ec.value();

19.5.4.6 Formatting [syserr.fmt]

template<class charT> struct formatter<error_code, charT> { constexpr void set_debug_format(); constexpr typename basic_format_parse_context<charT>::iterator parse(basic_format_parse_context<charT>& ctx); template<class Out> typename basic_format_context<Out, charT>::iterator format(const error_code& ec, basic_format_context<Out, charT>& ctx) const; };
constexpr void set_debug_format();
Effects: Modifies the state of the formatter to be as if the error-code-format-spec parsed by the last call to parse contained the ? option.
constexpr typename basic_format_parse_context<charT>::iterator parse(basic_format_parse_context<charT>& ctx);
Effects: Parses the format specifier as an error-code-format-spec and stores the parsed specifiers in *this.
The syntax of format specifications is
error-code-format-spec:
fill-and-align width ? s
where the productions fill-and-align and width are described in [format.string].
Returns: An iterator past the end of the error-code-format-spec.
template<class Out> typename basic_format_context<Out, charT>::iterator format(const error_code& ec, basic_format_context<Out, charT>& ctx) const;
Effects: If the s option is used, then:
  • If charT is char and the ordinary literal encoding is UTF-8, then let msg be ec.message() transcoded to UTF-8 with maximal subparts of ill-formed subsequences substituted with U+fffd replacement character per the Unicode Standard, Chapter 3.9 U+fffd Substitution in Conversion.
  • Otherwise, let msg be ec.message() transcoded to an implementation-defined encoding.
Otherwise, let msg be format("{}:{}", ec.category().name(), ec.value()).
If the ? option is used then msg is formatted as an escaped string ([format.string.escaped]).
Writes msg into ctx.out(), adjusted according to the error-code-format-spec.
Returns: An iterator past the end of the output range.