estd::result
Overview
estd::result
is a type used for returning and propagating errors. Similar
concepts exist in many other programming languages and implementations; so the
semantics are similar to:
Result<T, E>
in RustBoost.Outcome
P0323R3
std::expected
Either
in Haskell, Scala and other FP languages
In estd::result<T, E>
, T
is the type of value returned on success, and
E
is the error type, for example, an error code enum.
namespace
{
struct Error
{
uint32_t code;
explicit Error(uint32_t code) : code(code) {}
};
struct Foo
{
size_t bar;
Foo(size_t bar = 42) : bar(bar) {}
};
struct Pod
{
uint8_t t[10];
};
} // namespace
There is an edge case where memory consumption of result
can be optimized,
and that is when T
is void
and E
is an enum. It is possible to use
limited as internal storage, which typically results in
saving one word of memory. To do so, add a specialization of result_traits
next to enum type definition:
enum class Problem
{
NotEnoughChocolate,
JustBadLuck_IGuess,
};
namespace estd
{
template<>
struct result_traits<Problem>
{
static constexpr bool LIMITED = true;
};
} // namespace estd
where WhatWentWrong
is the error type used with result<void, E>
.