4 ms·
Here's my issue with UB in C/C++ (and I think it agrees with the article): I would much rather have direct compiler directives over the compiler being clever in
by overgard 4y ago
Here's my issue with UB in C/C++ (and I think it agrees with the article): I would much rather have direct compiler directives over the compiler being clever in a quiet way. The first example in this article is very good -- get_unchecked avoids undefined behavior (well, outside of accessing out of bounds), but still provides the wanted optimization. If something goes bad in your program, one of the first things you're going to suspect is functions with _unchecked at the end of them, so the developer ergonomics are great.
My main problem with undefined behavior is that it's rare to know when it's happening. I would much rather give hints to the compiler than having it "prove" something very subtle underneath the hood using UB, because then I understand where those things are happening. And it's not even like it's a rare thing! My C++ is littered with "const" and "[[nodiscard]]" and #pragma pack and all sorts of other things that don't change the behavior of the program but do indicate something important to the compiler. I want more of that, and less subtlety.