Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Prev Up HomeNext

TRY avoiding copy/move

BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr) works by creating an internal uniquely named variable which holds the value emitted by the expression. This implies that a copy or move operation shall be performed on the object emitted (unless you are on C++ 17 or later, which has guaranteed copy elision), which may be undesirable for your use case.

You can tell BOOST_OUTCOME_TRY to use a reference rather than a value for the internal uniquely named variable like this:

// This refers to a Result containing an immovable object
outcome::result<Immovable> &&res;

// For when you do want to extract the value
// This creates an auto &&unique = res, followed by an
// auto &&v = std::move(unique).assume_value()
BOOST_OUTCOME_TRY((auto &&, v), res);

If you don’t care about extracting the value:

// For when you don't want to extract the value
// This creates an auto &&unique = res
BOOST_OUTCOME_TRYV2(auto &&, res);

Last revised: February 12, 2021 at 20:25:43 UTC

Prev Up HomeNext