util: Add void support to util::Result
A minimal (but hacky) way to add support for void to Result originally posted https://github.com/bitcoin/bitcoin/pull/27632#discussion_r1195604095
This commit is contained in:
1 parent
51c050787f
commit
5f49cb1bc8
1 file changed
+4
-1
+4
-1
@@ -31,16 +31,19 @@ struct Error {
|
|||||||
//! `std::optional<T>` can be updated to return `util::Result<T>` and return
|
//! `std::optional<T>` can be updated to return `util::Result<T>` and return
|
||||||
//! error strings usually just replacing `return std::nullopt;` with `return
|
//! error strings usually just replacing `return std::nullopt;` with `return
|
||||||
//! util::Error{error_string};`.
|
//! util::Error{error_string};`.
|
||||||
template <class T>
|
template <class M>
|
||||||
class Result
|
class Result
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
using T = std::conditional_t<std::is_same_v<M, void>, std::monostate, M>;
|
||||||
|
|
||||||
std::variant<bilingual_str, T> m_variant;
|
std::variant<bilingual_str, T> m_variant;
|
||||||
|
|
||||||
template <typename FT>
|
template <typename FT>
|
||||||
friend bilingual_str ErrorString(const Result<FT>& result);
|
friend bilingual_str ErrorString(const Result<FT>& result);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {} // constructor for void
|
||||||
Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
|
Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
|
||||||
Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
|
Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user