blob: 8077baf6d37144048a1dd525c12dd72ee0ce31ac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#pragma once
#include <types.h>
// TODO T&& constructor
// TODO T&& Result::take() with consumed semantics
template <typename T, error_enum_t E>
class Result {
public:
explicit constexpr Result(T t) : is_ok(true), data{.t = t} {};
explicit constexpr Result(E e) : is_ok(false), data{.e = e} {};
constexpr operator bool() const { return is_ok; }
constexpr E error() const { return is_ok ? E::NoError : data.e; }
constexpr T value() const { return is_ok ? data.t : T{}; }
private:
const bool is_ok;
union {
T t;
E e;
} data;
};
|