skip to content

Why does REQUIRE(p.x == 3.0 && p.y == 4.0) fail to compile in Catch2, and what do you lose by wrapping it in parentheses?

level: seniorimportance: should knowfreq 35%

answer

  1. how both operands get printed
  2. an operator captures the left side
  3. && cannot keep short-circuiting
  4. parentheses evaluate first

basics

~20 s

Catch2 decomposes an assertion through overloaded operators to print both operands, and deliberately rejects && and || with a static_assert. Extra parentheses compile but turn the expression into one bool, so a failure prints only "false"; split it instead.

solid answer

~50 s

`REQUIRE(a == b)` expands to roughly `Decomposer{} <= a == b`. Because `<=` binds tighter than `==`, the decomposer captures `a` first, and Catch2's own `operator==` then builds an object holding both operands, which is how a failure prints `5.0 == 4.0`. Supporting `&&` and `||` would need overloads, and overloaded `&&` and `||` lose short-circuit evaluation, so `p && p->x == 3` would dereference a null pointer. Catch2 3 therefore defines them to trigger a `static_assert`; for `a == 3.0 && b == 4.0` the message is "chained comparisons are not supported inside assertions, wrap the expression inside parentheses, or decompose it". Wrapping it in an extra pair of parentheses makes the compiler evaluate it to a plain `bool` first, so it compiles, but a failure shows only `false`. Splitting it into `CHECK(p.x == 3.0); CHECK(p.y == 4.0);` keeps both expansions. `||` has no such split; a matcher or a rethink is better.

code

cpp · 11 lines
cpp
#include <catch2/catch_test_macros.hpp>

struct Point { double x, y; };

TEST_CASE("compound conditions", "[geometry]") {
    Point p{3, 4};
    // REQUIRE(p.x == 3.0 && p.y == 5.0);   // does not compile: static_assert
    CHECK((p.x == 3.0 && p.y == 5.0));      // compiles; failure prints only "false"
    CHECK(p.x == 3.0);                      // passes
    CHECK(p.y == 5.0);                      // fails with "4.0 == 5.0"
}

go deeper

for a junior

Recall that a failing REQUIRE prints the values on both sides of a comparison, and that && and || inside it do not compile.

for a middle

Explain the Decomposer trick with operator<= and why only one comparison can be captured per assertion.

for a senior

Explain the short-circuit argument, choose between splitting, parentheses and matchers, and recognise a failure that prints only false as lost diagnostics.

for a principal

Encourage assertion styles that keep failures self-explanatory, so triage of a red build does not start with adding print statements and rebuilding.

## What decomposition buys you A plain `assert(a == b)` can only tell you the expression was false. Catch2 prints the values on both sides: ```plaintext a.cpp:6: FAILED: CHECK( distance({0, 0}, {3, 4}) == 4.0 ) with expansion: 5.0 == 4.0 ``` That **expansion** is produced by **expression decomposition**: the macro rewrites the expression so that Catch2 sees the left operand, the operator and the right operand separately, instead of only the final `bool`. ## How it works The Catch2 3.16.1 source describes the idea in a comment: `REQUIRE( a == b )` becomes `Decomposer{} <= a == b`. 1. `operator<=` has **higher precedence** than `==`, so `Decomposer{} <= a` is evaluated first and wraps `a` in an `ExprLhs` object. Arithmetic binds tighter still, so in `a + b == c` the captured left side is `a + b`. 2. `ExprLhs` overloads `==`, `!=`, `<`, `>`, `<=` and `>=`. Calling one with the right operand produces a `BinaryExpr` holding both operands and the operator's spelling. 3. The assertion handler asks that object for its result and, on failure, stringifies both operands. This is why decomposition works for **one** comparison and stops there. ## Why && and || are refused To decompose `a == 1 && b == 2`, Catch2 would have to overload `operator&&`. An overloaded `&&` is an ordinary function call, so **both operands are always evaluated** and short-circuiting is lost. The documentation's example is the pointer idiom `p && p->foo == 2`: with the built-in operator, `p` is dereferenced only if it is non-null; with an overload, it is always dereferenced. Rather than compile something that silently changes meaning, Catch2 3 defines the operators so they fail at compile time: - After a comparison, as in `p.x == 3.0 && p.y == 4.0`, the left side is already a `BinaryExpr`, and clang 21 reported: `static assertion failed ... chained comparisons are not supported inside assertions, wrap the expression inside parentheses, or decompose it`. - With plain `bool` operands, as in `REQUIRE(a || b)`, the message is `operator|| is not supported inside assertions, wrap the expression inside parentheses, or decompose it`. The same "chained comparisons" error catches `REQUIRE(a == b == c)`, which in plain C++ compares a `bool` with `c` and is almost never what was meant. ## The two workarounds and their cost | Rewrite | Compiles | What a failure prints | |---|---|---| | `REQUIRE((p.x == 3.0 && p.y == 4.0))` | yes | `false` only | | `CHECK(p.x == 3.0); CHECK(p.y == 4.0);` | yes | `4.0 == 5.0` for the failing half | | A matcher combined with `&&` or `||` | yes | each matcher's description | The extra parentheses force the whole expression to be evaluated before the decomposer sees it, so Catch2 receives one `bool`. The same thing happens to `!(a == b)`: a leading `!` cannot be decomposed, which is why `REQUIRE_FALSE` exists. For `&&`, splitting is always possible and almost always better. For `||` there is no mechanical split. The documentation's own advice is that an `||` in a test expression is a reason to rethink the test; when an "either" condition is genuine, combining matchers with `||` keeps a readable description, as in a value that must be within a relative tolerance of a target or within an absolute margin of zero. ## Other limits worth knowing - **Commas.** The preprocessor splits macro arguments at top-level commas, so a multi-argument assertion such as `REQUIRE_THROWS_AS(std::pair<int, int>(1, 2), E)` sees three arguments. An alias for the type, or extra parentheses, fixes it. - **Printing.** Operands are printed through Catch2's `StringMaker`; a type with no `operator<<` and no `StringMaker` specialisation is still compared, but its value prints as `{?}`. - **Thread safety.** The documentation states that assertions are not thread-safe by default; Catch2 3.9.0 added experimental opt-in support. Asserting from worker threads needs that configuration or must be avoided. ## Reviewing assertions - A failure that says only `false` usually means a wrapped `&&`, a leading `!`, or a call returning `bool`; rewrite so a comparison is the outermost operator. - Prefer several narrow assertions to one compound one; each failure then names its own operands.

  • Why does Catch2 start decomposition with operator<= rather than some other operator?
    It needs an operator that binds tighter than the comparisons it wants to capture but looser than arithmetic. In `Decomposer{} <= a + b == c`, the addition happens first, then `<=` wraps `a + b`, and Catch2's overloaded `==` receives `c` and records both operands. A lower-precedence operator would let `==` evaluate to a plain `bool` before Catch2 saw either side.
  • How would you assert in Catch2 that a computed angle is either near zero or near a full turn?
    Use `REQUIRE_THAT` with matchers combined by `||`, for example `WithinAbs(0.0, 1e-9) || WithinAbs(full_turn, 1e-9)`, with `full_turn` holding two pi. Matchers overload `||` on matcher objects, not on the assertion's expression, so nothing short-circuits wrongly and the failure message lists both descriptions with the actual value.

  • Catch2 cannot print operand values; it only reports true or false.
  • Wrapping a && expression in parentheses keeps both operands in the report.
  • Catch2 could support && but simply never implemented it.
  • REQUIRE(a == b == c) checks that all three values are equal.
  • Splitting an || into two assertions is equivalent to the original.