Skip to content

Instantly share code, notes, and snippets.

@ShaneEverittM
Created June 27, 2021 09:53
Show Gist options
  • Save ShaneEverittM/255e272eab753d96f00381d1880a9246 to your computer and use it in GitHub Desktop.
Save ShaneEverittM/255e272eab753d96f00381d1880a9246 to your computer and use it in GitHub Desktop.
Simple Result
namespace DB {
class Result {
public:
enum Value : uint8_t {
Success,
Failure
};
constexpr Result() = default;
// Coerce from these type if necessary, useful for writing returning Result::Success as a Result not a Value.
// Do not want explicit constructions. Also will not show as used ever.
[[maybe_unused]] constexpr Result(Value aResult) : value(aResult) {}
[[maybe_unused]] constexpr Result(bool aBool) : value(aBool ? Success : Failure) {}
constexpr operator Value() const { return value; } // Allow switch and comparisons.
constexpr bool operator==(Result a) const { return value == a.value; }
constexpr bool operator!=(Result a) const { return value != a.value; }
[[nodiscard]] constexpr bool succeeded() const { return value == Success; }
[[nodiscard]] constexpr bool failed() const { return value == Failure; }
private:
Value value;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment