3 ms·
Exploring undefined behavior using constexpr
- hellofunk 8y agoI’ve recently been writing just pure C and have been surprised by how much I enjoy it. There are so many things about C++ that have remained confusing to me, even though I’ve used it extensively over the years and have a lot of respect for the language. But the simplicity not having to wrap my head around all kinds of bizarre concepts that still confuse me in C++, I must say it’s been a bit liberating of a feeling.
- ToolsDevler 8y agoI had the same expirience and I am totally with you. At work we use modern C++ which I enjoy, but I have the same liberating and comfortable feeling when I turn back to my small C projects at home. But I also think that huge C Projects can turn into a hell easily.
- blub 8y agoI guess writing it can be fun, but managing it isn't. When I review C code I have to spend 80% of my attention on figuring out if there's anything fishy happening with memory. It's terrible how much C code is just memory-management boilerplate.
- saagarjha 8y agoThis is a super interesting approach I hadn’t thought of. Unfortunately it seems to be limited in practice, as demonstrated by the article: first of all, certain constructs cannot be used in a constexpr context, and second (and more worryingly) many compilers fail to diagnose undefined behavior or do so incorrectly. If the second issue can be fixed I can see this being a very useful, if specialized tool for improving the safety of C++ code.
- jcelerier 8y ago> certain constructs cannot be used in a constexpr context yet. endgame is to make mostly everything constexpr-able. constexpr dynamic_cast, try/catch, and virtuals are coming in C++20 for instance and there are papers to get constexpr memory allocations in C++23.
- bregma 8y ago> many compilers fail to diagnose undefined behavior or do so incorrectly. It would be much easier if undefined behaviour was better defined, because then a compiler could recognize it and diagnose it. Of course then it wouldn't be undefined behaviour any more, so problem solved.
- saagarjha 8y agoThe bounds of what consists undefined behavior is, well, defined. The “undefined” bit refers to the behavior in this case, not its ability to be recognized.