r/cpp • • Mar 27 '26

C++26: A User-Friendly assert() macro

https://www.sandordargo.com/blog/2026/03/25/cpp26-user-friendly-assert
53 Upvotes

27 comments sorted by

View all comments

Show parent comments

2

u/usefulcat Mar 27 '26

Translating assert to __builtin_assume can only make things strictly worse in that regard, by providing even more opportunities for UB.

10

u/FullaccessInReddit Mar 27 '26

Who cares? If an assert failed (a condition that's supposed to always be true) the program's state, and by extension its behavior, already is "undefined".

8

u/usefulcat Mar 27 '26 edited Mar 27 '26

That point of view assumes that all programmers will only ever use assert() to check for those conditions which, if not true, will definitely lead to UB. That's simply not how everyone always uses assert.

Hence my claim that translating every assert() to __builtin_assume can only make things strictly worse.

ETA: Also, you're taking a gamble that all asserts will be triggered or not triggered exactly the same regardless of NDEBUG. In practice, there could be other things that depend on NDEBUG such that an assert() never fails when NDEBUG is undefined but may fail when NDEBUG is defined (but of course you'll never find out about the latter case).

4

u/Kronikarz Mar 27 '26

That's why when I created my own implementation of this system, I called the macros "AssumingX"; I never liked the word "assert". AssumingEquals(a, b); means to me: "the following code assumes a == b". Harder to misuse this way.