C++26: A User-Friednly assert() macro (sandordargo.com)

by jandeboevrie 82 comments 68 points
Read article View on HN

82 comments

[−] WalterBright 48d ago
D just makes assert() part of the language:

https://dlang.org/spec/expression.html#assert_expressions

The behavior of it can be set with a compiler switch to one of:

1. Immediately halting via execution of a special CPU instruction

2. Aborting the program

3. Calling the assert failure function in the corresponding C runtime library

4. Throwing the AssertError exception in the D runtime library

So there's no issue with parsing it. The compiler also understands the semantics of assert(), and so things like assert(0) can be recognized as being the end of the program.

[−] rurban 48d ago
So you are ignoring our well beloved NDEBUG? :)

Our idea of declare (optimize (speed 3) (safety 0))

[−] WalterBright 47d ago
D compilers have a "ramming speed" setting.
[−] panzi 46d ago
But does it have a ludicrous speed setting?
[−] MontagFTB 48d ago
Putting code with side effects into an assert is asking for trouble. Compile with NDEBUG set and the effects mysteriously disappear! Anything beyond an equality expression or straight boolean should be avoided.
[−] nyc_pizzadev 48d ago
The nice thing about assert() is you can just define your own:

https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a79...

In this case, the ability to see the actual values that triggered the assert is way more helpful.

[−] omoikane 48d ago

> (assert) doesn't follow the usual SCREAMING_SNAKE_CASE convention we associate with macros

There are a few things like that, for example:

https://en.cppreference.com/w/c/numeric/math/isnan - isnan is an implementation defined macro.

https://en.cppreference.com/w/c/io/fgetc - getc may be implemented as a macro, but often it's a function.

[−] grokcodec 48d ago
Friedns shouldn't let Freidns post on HN without running spell check
[−] amelius 48d ago
Shouldn't the preprocessor be fixed, if it trips that easily on common C++ constructs?
[−] adzm 48d ago
One of my favorite things from ATL/WTL was the _ASSERT_E macro which additionally converts the source expression to text for a better message to be logged
[−] wpollock 48d ago

> assert(x > 0 && "x was not greater than zero");

Shouldn't that be "||" rather than "&&"? We want the message only if the boolean expression is false.

[−] throwpoaster 48d ago
assert(spellcheck(“Friednly”));
[−] semiinfinitely 48d ago
"C++47: Finally, a Standard Way to Split a String by Delimiter"