Skip to content

Replace boost::variant with std::variant

SlowRiot requested to merge SlowRiot/bitcoin-cash-node:boost_variant into master

Part of #390 - Boost elimination.

This MR replaces boost::variant with C++17's std::variant. The two have equivalent functionality, but their interfaces differ, so the changes are fairly extensive. Switching to std::variant allows for much cleaner and more compact code, with much less boilerplate - no need to create helper classes for each visitor pattern, and possible to do away with the DstTypeChecker helper. Some minor cleanup is also carried out in the functions associated with the visitors.

310 lines of associated with boost::variant have been replaced with 206 using std::variant. No changes to behaviour.

Changes:

  • Remove visitor functors, move logic to lambdas in the function previously calling them, or to a single standalone function if called from multiple places.
  • Replace error-prone which() == [integer] method of identifying the held type in a variant, with the std::holds_alternative template.
  • Descriptive comments showing explicitly when variant branches are intended to take no action.
  • Add static asserts to fail compilation if any visitors fail to handle any of the possible variant types.
    • Add a always_false_v template helper in script/standard.h for this purpose.
  • Distinguish between comparing variant types and variant contents in the tests.
  • Replace boost::get with either std::get or std::get_if depending on intended behaviour.
  • Make IsValidDestination constexpr, and move it to the script/standard.h header.
  • Remove boost variant includes.
  • Update linters:
    • Remove 3 boost variant related headers from expected includes list
    • Remove old check against use of std::variant
    • Add check against use of boost::variant
  • Adjacent changes not directly related to boost elimination:
    • Add descriptive comments to relevant generated test cases in cashaddrenc_tests which work with variant comparisons.
    • Remove branches over include_addresses that is always set to true in rpcwallet logic dispatched by variant (presumably due to partial backport), and remove associated comment.

Test plan:

ninja check-bitcoin
ninja check-functional

This change needs to be verified on MacOS especially - if std::variant is not available, that would be a showstopper for now.

Merge request reports