Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_RESULT_H6#define BITCOIN_UTIL_RESULT_H8#include <attributes.h>9#include <util/translation.h>11#include <variant>13namespace util {15struct Error {16bilingual_str message;17};19//! The util::Result class provides a standard way for functions to return20//! either error messages or result values.21//!22//! It is intended for high-level functions that need to report error strings to23//! end users. Lower-level functions that don't need this error-reporting and24//! that only need error-handling should avoid util::Result and instead use25//! util::Expected, std::optional, std::variant, or custom structs26//! and enum types to return function results.27//!28//! Usage examples can be found in \example ../test/result_tests.cpp, but in29//! general code returning `util::Result<T>` values is very similar to code30//! returning `std::optional<T>` values. Existing functions returning31//! `std::optional<T>` can be updated to return `util::Result<T>` and return32//! error strings usually just replacing `return std::nullopt;` with `return33//! util::Error{error_string};`.34template <class M>35class Result36{37private:38using T = std::conditional_t<std::is_same_v<M, void>, std::monostate, M>;40std::variant<bilingual_str, T> m_variant;42//! Disallow copy constructor, require Result to be moved for efficiency.43Result(const Result&) = delete;45//! Disallow operator= to avoid confusion in the future when the Result46//! class gains support for richer error reporting, and callers should have47//! ability to set a new result value without clearing existing error48//! messages.49Result& operator=(const Result&) = delete;50Result& operator=(Result&&) = delete;52template <typename FT>53friend bilingual_str ErrorString(const Result<FT>& result);55public:56Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {} // constructor for void57Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}58Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}59Result(Result&&) = default;60~Result() = default;62//! std::optional methods, so functions returning optional<T> can change to63//! return Result<T> with minimal changes to existing code, and vice versa.64bool has_value() const noexcept { return m_variant.index() == 1; }65const T& value() const LIFETIMEBOUND66{67assert(has_value());68return std::get<1>(m_variant);69}70T& value() LIFETIMEBOUND71{72assert(has_value());73return std::get<1>(m_variant);74}75template <class U>76T value_or(U&& default_value) const&77{78return has_value() ? value() : std::forward<U>(default_value);79}80template <class U>81T value_or(U&& default_value) &&82{83return has_value() ? std::move(value()) : std::forward<U>(default_value);84}85explicit operator bool() const noexcept { return has_value(); }86const T* operator->() const LIFETIMEBOUND { return &value(); }87const T& operator*() const LIFETIMEBOUND { return value(); }88T* operator->() LIFETIMEBOUND { return &value(); }89T& operator*() LIFETIMEBOUND { return value(); }90};92template <typename T>93bilingual_str ErrorString(const Result<T>& result)94{95return result ? bilingual_str{} : std::get<0>(result.m_variant);96}97} // namespace util99#endif // BITCOIN_UTIL_RESULT_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.