optional.h – Commented Bitcoin source code

optional.h – Commented Bitcoin source code

optional.h is a stub that’ll likely get removed from Bitcoin Core at a future date when C++ 17 gets formally adopted for the entire project.

By reading the Bitcoin Core sources you can see that lots of sections could use C++ 17 idioms.

Let’s keep in mind though that Bitcoin Core is high security software that manages hundreds of billions of dollars in value. Changing compiler or language versions is a major step that involves all sorts of security concerns. We must therefore adapt little stubs like optional.h here and there in order to keep the progress into future versions secure and carefully planned.


#include <boost/optional.hpp>

//! Substitute for C++17 std::optional
template <typename T>
using Optional = boost::optional<T>;

//! Substitute for C++17 std::make_optional
template <typename T>
Optional<T> MakeOptional(bool condition, T&& value)
    return boost::make_optional(condition, std::forward<T>(value));

//! Substitute for C++17 std::nullopt
static auto& nullopt = boost::none;


First #include <boost/optional.hpp>is included.

I haven’t usually quoted any #include‘s from the source code files but this time it’s relevant since all this file does is wrap boost library’s optional type.

Next it defines Optional<T> as a template for a synonym to boost::optional<T>. This abstraction will allow the entire source code base to shift to C++ 17’s implementation by changing just this line.

Now we wrapt boost::make_optional with our own MakeOptional. Same idea as before – when C++ 17 is adopted the standard library will provide this method.

Lastly the optional “no value” return type is defined static auto& nullopt = boost::none;

Return to Bitcoin Core source code commentary index


optional.h File Reference

Discussion: upgrading to C++17 #16684