MRDOCS_CHECK_OR_BREAK

Check a value and leave the enclosing loop if it failed.

Synopsis

#define MRDOCS_CHECK_OR_BREAK(var)

Description

If the value failed, the macro runs break. See MRDOCS_TRY for what counts as failed.

// Count the leading results that succeeded.
std::size_t n = 0;
for (Expected<int> const& r : results)
{
    MRDOCS_CHECK_OR_BREAK(r);   // stop at the first error
    ++n;
}

The loop expands to roughly this (simplified: the real code calls ::mrdocs::detail::failed):

std::size_t n = 0;
for (Expected<int> const& r : results)
{
    // MRDOCS_CHECK_OR_BREAK(r);
    if (failed(r)) {   // !r, since r is an Expected
        break;
    }

    ++n;
}

The value is evaluated once, so a call expression is fine.

Things to keep in mind:

  • Use it only inside a loop or switch, and end it with a semicolon.

  • It's a plain break, so inside a switch it leaves the switch, not the loop around it.

  • The expansion is two statements. Put braces around it when it's the body of an if or else.

Parameters

Name

Description

var

The value to check.

Created with MrDocs