Skip to content

Commit 5f49cb1

Browse files
author
MarcoFalke
committed
util: Add void support to util::Result
A minimal (but hacky) way to add support for void to Result originally posted bitcoin/bitcoin#27632 (comment)
1 parent 51c0507 commit 5f49cb1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/util/result.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ struct Error {
3131
//! `std::optional<T>` can be updated to return `util::Result<T>` and return
3232
//! error strings usually just replacing `return std::nullopt;` with `return
3333
//! util::Error{error_string};`.
34-
template <class T>
34+
template <class M>
3535
class Result
3636
{
3737
private:
38+
using T = std::conditional_t<std::is_same_v<M, void>, std::monostate, M>;
39+
3840
std::variant<bilingual_str, T> m_variant;
3941

4042
template <typename FT>
4143
friend bilingual_str ErrorString(const Result<FT>& result);
4244

4345
public:
46+
Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {} // constructor for void
4447
Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
4548
Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
4649

0 commit comments

Comments
 (0)