1// <system_error> -*- C++ -*-
3// Copyright (C) 2007-2023 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/system_error
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_SYSTEM_ERROR
30#define _GLIBCXX_SYSTEM_ERROR 1
32#pragma GCC system_header
34#include <bits/requires_hosted.h> // OS-dependent
36#if __cplusplus < 201103L
37# include <bits/c++0x_warning.h>
40#include <bits/c++config.h>
41#include <bits/error_constants.h>
44#if __cplusplus > 201703L
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 /** @addtogroup diagnostics
57 class error_condition;
60 /// is_error_code_enum
61 template<typename _Tp>
62 struct is_error_code_enum : public false_type { };
64 /// is_error_condition_enum
65 template<typename _Tp>
66 struct is_error_condition_enum : public false_type { };
69 struct is_error_condition_enum<errc>
70 : public true_type { };
72#if __cplusplus > 201402L
73 template <typename _Tp>
74 inline constexpr bool is_error_code_enum_v =
75 is_error_code_enum<_Tp>::value;
76 template <typename _Tp>
77 inline constexpr bool is_error_condition_enum_v =
78 is_error_condition_enum<_Tp>::value;
82_GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2)
84 /** @addtogroup diagnostics
88 /** Abstract base class for types defining a category of error codes.
90 * An error category defines a context that gives meaning to the integer
91 * stored in an `error_code` or `error_condition` object. For example,
92 * the standard `errno` constants such a `EINVAL` and `ENOMEM` are
93 * associated with the "generic" category and other OS-specific error
94 * numbers are associated with the "system" category, but a user-defined
95 * category might give different meanings to the same numerical values.
97 * A user-defined category can override the `equivalent` member functions
98 * to define correspondence between errors in different categories.
99 * For example, a category for errors from disk I/O could consider some
100 * of its error numbers equivalent to ENOSPC and ENOENT in the generic
103 * @headerfile system_error
109 constexpr error_category() noexcept = default;
111 virtual ~error_category();
113 error_category(const error_category&) = delete;
114 error_category& operator=(const error_category&) = delete;
116 /// A string that identifies the error category.
118 name() const noexcept = 0;
120 // We need two different virtual functions here, one returning a
121 // COW string and one returning an SSO string. Their positions in the
122 // vtable must be consistent for dynamic dispatch to work, but which one
123 // the name "message()" finds depends on which ABI the caller is using.
124#if _GLIBCXX_USE_CXX11_ABI
126 _GLIBCXX_DEFAULT_ABI_TAG
128 _M_message(int) const;
131 /// A description of the error condition corresponding to the number.
132 _GLIBCXX_DEFAULT_ABI_TAG
134 message(int) const = 0;
137 message(int) const = 0;
141 _M_message(int) const;
145 /// Return an error_condition corresponding to `i` in this category.
146 virtual error_condition
147 default_error_condition(int __i) const noexcept;
149 /// Test whether `cond` corresponds to `i` for this category.
151 equivalent(int __i, const error_condition& __cond) const noexcept;
153 /// Test whether `code` corresponds to `i` for this category.
155 equivalent(const error_code& __code, int __i) const noexcept;
157 /// An error_category only compares equal to itself.
160 operator==(const error_category& __other) const noexcept
161 { return this == &__other; }
163 /// Ordered comparison that defines a total order for error categories.
164#if __cpp_lib_three_way_comparison
167 operator<=>(const error_category& __rhs) const noexcept
168 { return std::compare_three_way()(this, &__rhs); }
171 operator<(const error_category& __other) const noexcept
172 { return less<const error_category*>()(this, &__other); }
175 operator!=(const error_category& __other) const noexcept
176 { return this != &__other; }
182 /// Error category for `errno` error codes.
183 [[__nodiscard__, __gnu__::__const__]]
184 const error_category&
185 generic_category() noexcept;
187 /// Error category for other error codes defined by the OS.
188 [[__nodiscard__, __gnu__::__const__]]
189 const error_category&
190 system_category() noexcept;
194_GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
196 /** @addtogroup diagnostics
202 void make_error_code() = delete;
203 void make_error_condition() = delete;
208 * This class is a value type storing an integer error number and a
209 * category that gives meaning to the error number. Typically this is done
210 * close the the point where the error happens, to capture the original
213 * An `error_code` object can be used to store the original error value
214 * emitted by some subsystem, with a category relevant to the subsystem.
215 * For example, errors from POSIX library functions can be represented by
216 * an `errno` value and the "generic" category, but errors from an HTTP
217 * library might be represented by an HTTP response status code (e.g. 404)
218 * and a custom category defined by the library.
220 * @headerfile system_error
225 template<typename _ErrorCodeEnum>
227 = __enable_if_t<is_error_code_enum<_ErrorCodeEnum>::value>;
230 error_code() noexcept
231 : _M_value(0), _M_cat(&system_category()) { }
233 error_code(int __v, const error_category& __cat) noexcept
234 : _M_value(__v), _M_cat(&__cat) { }
236 /// Initialize with a user-defined type, by calling make_error_code.
237 template<typename _ErrorCodeEnum,
238 typename = _Check<_ErrorCodeEnum>>
239 error_code(_ErrorCodeEnum __e) noexcept
241 using __adl_only::make_error_code;
242 *this = make_error_code(__e);
245 error_code(const error_code&) = default;
246 error_code& operator=(const error_code&) = default;
249 assign(int __v, const error_category& __cat) noexcept
257 { assign(0, system_category()); }
262 value() const noexcept { return _M_value; }
264 /// The error category that this error belongs to.
266 const error_category&
267 category() const noexcept { return *_M_cat; }
269 /// An `error_condition` for this error's category and value.
271 default_error_condition() const noexcept;
273 /// The category's description of the value.
274 _GLIBCXX_DEFAULT_ABI_TAG
277 { return category().message(value()); }
279 /// Test whether `value()` is non-zero.
281 explicit operator bool() const noexcept
282 { return _M_value != 0; }
287 const error_category* _M_cat;
290 // C++11 19.5.2.5 non-member functions
292 /** Create an `error_code` representing a standard `errc` condition.
294 * The `std::errc` constants correspond to `errno` macros and so use the
297 * @relates error_code
302 make_error_code(errc __e) noexcept
303 { return error_code(static_cast<int>(__e), generic_category()); }
305 /** Ordered comparison for std::error_code.
307 * This defines a total order by comparing the categories, and then
308 * if they are equal comparing the values.
310 * @relates error_code
313#if __cpp_lib_three_way_comparison
315 inline strong_ordering
316 operator<=>(const error_code& __lhs, const error_code& __rhs) noexcept
318 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
320 return __lhs.value() <=> __rhs.value();
324 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
326 return (__lhs.category() < __rhs.category()
327 || (__lhs.category() == __rhs.category()
328 && __lhs.value() < __rhs.value()));
332 /** Write a std::error_code to an ostream.
334 * @relates error_code
337 template<typename _CharT, typename _Traits>
338 basic_ostream<_CharT, _Traits>&
339 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
340 { return (__os << __e.category().name() << ':' << __e.value()); }
342 /** Class error_condition
344 * This class represents error conditions that may be visible at an API
345 * boundary. Different `error_code` values that can occur within a library
346 * or module might map to the same `error_condition`.
348 * An `error_condition` represents something that the program can test for,
349 * and subsequently take appropriate action.
351 * @headerfile system_error
354 class error_condition
356 template<typename _ErrorConditionEnum>
358 = __enable_if_t<is_error_condition_enum<_ErrorConditionEnum>::value>;
361 /// Initialize with a zero (no error) value and the generic category.
362 error_condition() noexcept
363 : _M_value(0), _M_cat(&generic_category()) { }
365 /// Initialize with the specified value and category.
366 error_condition(int __v, const error_category& __cat) noexcept
367 : _M_value(__v), _M_cat(&__cat) { }
369 /// Initialize with a user-defined type, by calling make_error_condition.
370 template<typename _ErrorConditionEnum,
371 typename = _Check<_ErrorConditionEnum>>
372 error_condition(_ErrorConditionEnum __e) noexcept
374 using __adl_only::make_error_condition;
375 *this = make_error_condition(__e);
378 error_condition(const error_condition&) = default;
379 error_condition& operator=(const error_condition&) = default;
381 /// Set the value and category.
383 assign(int __v, const error_category& __cat) noexcept
389 /// Reset the value and category to the default-constructed state.
392 { assign(0, generic_category()); }
394 // C++11 19.5.3.4 observers
399 value() const noexcept { return _M_value; }
401 /// The error category that this error belongs to.
403 const error_category&
404 category() const noexcept { return *_M_cat; }
406 /// The category's description of the value.
407 _GLIBCXX_DEFAULT_ABI_TAG
410 { return category().message(value()); }
412 /// Test whether `value()` is non-zero.
414 explicit operator bool() const noexcept
415 { return _M_value != 0; }
420 const error_category* _M_cat;
423 // C++11 19.5.3.5 non-member functions
425 /** Create an `error_condition` representing a standard `errc` condition.
427 * The `std::errc` constants correspond to `errno` macros and so use the
430 * @relates error_condition
434 inline error_condition
435 make_error_condition(errc __e) noexcept
436 { return error_condition(static_cast<int>(__e), generic_category()); }
438 // C++11 19.5.4 Comparison operators
440 /** Equality comparison for std::error_code.
442 * Returns true only if they have the same category and the same value.
444 * @relates error_condition
449 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
451 return __lhs.category() == __rhs.category()
452 && __lhs.value() == __rhs.value();
455 /** Equality comparison for std::error_code and std::error_condition.
457 * Uses each category's `equivalent` member function to check whether
458 * the values correspond to an equivalent error in that category.
460 * @relates error_condition
465 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
467 return __lhs.category().equivalent(__lhs.value(), __rhs)
468 || __rhs.category().equivalent(__lhs, __rhs.value());
471 /** Equality comparison for std::error_condition.
473 * Returns true only if they have the same category and the same value.
475 * @relates error_condition
480 operator==(const error_condition& __lhs,
481 const error_condition& __rhs) noexcept
483 return __lhs.category() == __rhs.category()
484 && __lhs.value() == __rhs.value();
487 /** Ordered comparison for std::error_condition.
489 * This defines a total order by comparing the categories, and then
490 * if they are equal comparing the values.
492 * @relates error_condition
495#if __cpp_lib_three_way_comparison
497 inline strong_ordering
498 operator<=>(const error_condition& __lhs,
499 const error_condition& __rhs) noexcept
501 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
503 return __lhs.value() <=> __rhs.value();
507 operator<(const error_condition& __lhs,
508 const error_condition& __rhs) noexcept
510 return (__lhs.category() < __rhs.category()
511 || (__lhs.category() == __rhs.category()
512 && __lhs.value() < __rhs.value()));
515 /// @relates error_condition
517 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
519 return (__rhs.category().equivalent(__rhs.value(), __lhs)
520 || __lhs.category().equivalent(__rhs, __lhs.value()));
523 /// @relates error_code
525 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
526 { return !(__lhs == __rhs); }
528 /// @relates error_code
530 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
531 { return !(__lhs == __rhs); }
533 /// @relates error_condition
535 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
536 { return !(__lhs == __rhs); }
538 /// @relates error_condition
540 operator!=(const error_condition& __lhs,
541 const error_condition& __rhs) noexcept
542 { return !(__lhs == __rhs); }
543#endif // three_way_comparison
547 * @brief An exception type that includes an `error_code` value.
549 * Typically used to report errors from the operating system and other
552 * @headerfile system_error
554 * @ingroup exceptions
556 class system_error : public std::runtime_error
562 system_error(error_code __ec = error_code())
563 : runtime_error(__ec.message()), _M_code(__ec) { }
565 system_error(error_code __ec, const string& __what)
566 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
568 system_error(error_code __ec, const char* __what)
569 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
571 system_error(int __v, const error_category& __ecat, const char* __what)
572 : system_error(error_code(__v, __ecat), __what) { }
574 system_error(int __v, const error_category& __ecat)
575 : runtime_error(error_code(__v, __ecat).message()),
576 _M_code(__v, __ecat) { }
578 system_error(int __v, const error_category& __ecat, const string& __what)
579 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
580 _M_code(__v, __ecat) { }
582#if __cplusplus >= 201103L
583 system_error (const system_error &) = default;
584 system_error &operator= (const system_error &) = default;
587 virtual ~system_error() noexcept;
590 code() const noexcept { return _M_code; }
593_GLIBCXX_END_NAMESPACE_VERSION
596#include <bits/functional_hash.h>
598namespace std _GLIBCXX_VISIBILITY(default)
600_GLIBCXX_BEGIN_NAMESPACE_VERSION
602#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
604 /// std::hash specialization for error_code.
605 /// @relates error_code
607 struct hash<error_code>
608 : public __hash_base<size_t, error_code>
611 operator()(const error_code& __e) const noexcept
613 const size_t __tmp = std::_Hash_impl::hash(__e.value());
614 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
617#endif // _GLIBCXX_COMPATIBILITY_CXX0X
619#if __cplusplus >= 201703L
621 /// std::hash specialization for error_condition.
622 /// @relates error_condition
624 struct hash<error_condition>
625 : public __hash_base<size_t, error_condition>
628 operator()(const error_condition& __e) const noexcept
630 const size_t __tmp = std::_Hash_impl::hash(__e.value());
631 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
636_GLIBCXX_END_NAMESPACE_VERSION
641#endif // _GLIBCXX_SYSTEM_ERROR