From b7f5d7565a19efbfebd72f2877444c552e4230c8 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 12 Mar 2021 16:44:02 +0200 Subject: libk: add Result class --- libk/result.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 libk/result.h (limited to 'libk/result.h') diff --git a/libk/result.h b/libk/result.h new file mode 100644 index 0000000..8077baf --- /dev/null +++ b/libk/result.h @@ -0,0 +1,23 @@ +#pragma once +#include + +// TODO T&& constructor +// TODO T&& Result::take() with consumed semantics + +template +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; +}; -- cgit v1.2.1