| ▲ | MontagFTB 5 hours 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. | |||||||||||||||||||||||||||||
| ▲ | samiv 3 hours ago | parent | next [-] | ||||||||||||||||||||||||||||
That's why you define your own assert macro and keep in on unconditionally. Your programs will be better for it. | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| ▲ | usrnm 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
I once spent several days debugging that same mistake. Stuff worked perfectly in tests but broke misteriously in production builds. Couldn't stop laughing for a few minutes when I finally figured it out. | |||||||||||||||||||||||||||||
| ▲ | maccard 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
Indeed.
Does not do what you think it does with nullptr. A major game engine [0] has a toggle to enable asserts in shipping builds, mostly for this reason[0] https://dev.epicgames.com/documentation/en-us/unreal-engine/... | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| ▲ | nyc_pizzadev 5 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
This is just a symptom of a bad assert() implementation, which funny enough is the standard. If you properly (void) it out, side effects are maintained. https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a79... | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| ▲ | jmalicki 5 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
Side effects are bad of course, but anything beyond a straight boolean or equality is bad? `assert(vector.size() < 3)` is ridiculous to you? | |||||||||||||||||||||||||||||
| ▲ | andrepd 35 minutes ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
Rust has assert and debug_assert, which are self-explanatory. But it also has an assert_unchecked, which is what other languages incl C++ call an "assume" (meaning "this condition not holding is undefined behaviour"), with the added bonus that debug builds assert that the condition is true. | |||||||||||||||||||||||||||||
| ▲ | nealabq 4 hours ago | parent | prev [-] | ||||||||||||||||||||||||||||
I don't mean to be that guy, but for "functional" programmers a print statement has "side effects". But your meaning is clear. In an assert expression, don't call functions that might change the program/database state. Be as "const" as possible. | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||