Major differences
The major design differences between <system_error>
and proposed <system_error2>
are
as follows:
experimental::status_code<DomainType>
can represent warnings and form-of-success codes as well as failure codes.experimental::errored_status_code<DomainType>
is more similar tostd::error_code
, in that it can only represent failures (this is enforced by C++ 20 contract or runtime assertion check).The code’s domain implementation defines the payload type to be transported around by
experimental::status_code<DomainType>
, rather than it being hardcoded toint
as instd::error_code
. The payload type can be anything you like, including non-trivially-copyable, move-only, complex etc types.This facility is extremely useful. Extra failure metadata such as stack backtraces can be returned, for example. You can absolutely vary the payload depending on whether
NDEBUG
or_DEBUG
is defined, too.If your domain defines a payload type which is trivially copyable or move relocating1, it gains an implicit convertibility to a move-only
experimental::status_code<erased<T>>
whereT
is another trivially copyable or move relocating type. This permits global headers to use a single, common, type erased, status code type which is highly desirable for code bases of any complexity. However, unlikestd::error_code
, which fulfils the exact same role in<system_error>
based code, the type erased payload can be bigger than the hardcodedint
instd::error_code
.This facility is also extremely useful, as extra failure metadata can be type erased, transported through code with no knowledge of such things, and the original type with failure metadata resurrected at the handling point. Indeed P1095 proposed
std::error
is a type alias toexperimental::status_code<erased<intptr_t>>
, and it can transport erasedstd::exception_ptr
instances, POSIX error codes, and much more besides.Equality comparisons between status code’s with non-identical domains are always semantic i.e. are they semantically equivalent, rather than exactly equal? This mirrors when you compare
std::error_code
to astd::error_condition
, but as there is no equivalent for the latter in<system_error2>
, this means that when you see the code:if(code1 == code2) ...
… you can be highly confident that this is an inexact, semantic, match operation. The same code under
<system_error>
is highly ambiguous as to whether exact or inexact comparison is being performed (after all, all there is is “code1 == code2
”, so it depends on the types ofcode1
andcode2
which usually is not obvious).The ambiguity, and high cognitive load during auditing
<system_error>
code for correctness, has led to many surprising and unexpected failure handling bugs during the past decade in production C++.<system_error2>
, being a new design, has all-constexpr construction and destruction which avoids the need for static global variables, as<system_error>
has. Each of those static global variables requires an atomic fence just in case it has not been initialised, thus every retrieval of an error category bloats code and inhibits optimisation, plus makes the use of custom error code categories in header-only libraries unreliable. Boost.System has replicated the all-constexpr construction and destruction from<system_error2>
, and thus now has similar characteristics in this regard.Finally, this is a small but important difference. Under
<system_error>
, this extremely common use case is ambiguous:if(ec) ...
Does this code mean “if there was an error?”, or “if the error code is set?”, or “is the error code non-zero?”. The correct answer according to the standard is the last choice, but a quick survey of open source
<system_error>
based code on github quickly demonstrates there is widespread confusion regarding correct usage.<system_error2>
solves this by removing the boolean test entirely. One now writesif(sc.success()) ...
,if(sc.failure()) ...
,if(sc.empty()) ...
and so on, thus eliminating ambiguity.
- Move relocating is not in the standard, it is currently within WG21 Evolution Working Group Incubator. It is defined to be a type whose move constructor
memcpy()
’s the bits from source to destination, followed bymemcpy()
of the bits of a default constructed instance to source, and with a programmer-given guarantee that the destructor, when called on a default constructed instance, has no observable side effects. A surprising number of standard library types can meet this definition of move relocating, includingstd::vector<T>
,std::shared_ptr<T>
, andstd::exception_ptr
. [return]