Question Details

No question body available.

Tags

c++ templates constexpr static-assert

Answers (1)

Accepted Answer Available
Accepted Answer
April 30, 2025 Score: 7 Rep: 44,810 Quality: Expert Completeness: 80%
staticassert(sumimpl(value, rest...), "Overflow");

This cannot possibly work because value and rest aren't constant expressions. Just because this staticassert is inside a constexpr function doesn't mean that you could read out their values within staticassert.

See also Can't use function parameter of a constexpr function in a constant expression

staticassert(false, "Overflow"); and delegatedassert(); are obviously going to fail as well. staticassertions are evaluated at compile time, and it is irrelevant whether you put them into an if statement. You can only have such "conditional staticasserts in an if constexpr statement, but that wouldn't work for the same reason that the aforementioned staticassert always fails.

In general, the staticasserts seem unnecessary here. If an overflow is detected, an exception will be thrown, and

constexpr int a = sum(10, 20, 30);

... won't compile.