Checking the usages of assert(0) in my code it is used to create a
breakpoint in unusual situations. Sample
if (condition) {
}
else {
assert(0); //unexpected
return 1;
}
I am thinking in replacing all my assert(0) for this macro.
#define dbg_break(message) assert(! "dbg_break" message );
if (condition) {
}
else {
dbg_break("unexpected");
return 1;
}
On 06/06/2024 17:10, Blue-Maned_Hawk wrote:
I feel like you would probably want to be using a debugger to set your
breakpoints, although the Clang dialect of C does have a
__builtin_debugtrap builtin subroutine.
C++ 26 will have breakpoint. Lot of links about how to do that. https://en.cppreference.com/w/cpp/utility/breakpoint
But, thinking about my usage, the source line, message, etc., everything
that assert has is useful. What is not useful is to pass a constant expression for something that is supposed to be a runtime check.
I remember when I thought that static_assert could just be assert
because it is not hard for the compiler to know when we have a constant expression or not. If we have a constant expression, this should be just
like static_assert.
assert(2 == 2); // compile-time check
If the expression is not constant, then it would be checked at runtime
during debugging.
This idea fails fast, when we think assert is used with assert(0);
I think dbg_break also transmit the idea that the branch is possible
while assert(0) can be confusing.
On 06/06/2024 22:40, Thiago Adams wrote:
If the expression is not constant, then it would be checked at runtime
during debugging.
This idea fails fast, when we think assert is used with assert(0);
Personally, I can't see any use for an assertion that is always known to >fail.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 146:33:51 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,708 |