Modern C for C++ Peeps
floooh.github.io
floooh.github.io
Also it's impractical to only have POD structs i.e. structs with no pointer pointing to the free store.
Good otherwise.
Nice colours and theming though :)
The part about the author's take on RAII is interesting, but it would really merit a separate post with code examples.
Minor nit: > there’s now a standardized bool with true/false (also not built-in but defined in stdbool.h)
C99 introduced _Bool, which is a built-in.
stdbool.h defines bool as an alias to it (unless compiling as C++, which then gets left alone as the native C++ bool).
No kidding!
Still, anybody coding C is still, within epsilon, coding C++, but badly.
But even before the newer C++ became widespread, I wondered why it lacks behind C99 in some aspects. And it seems that this hasn't changed much:
> The good news is that C++20 is getting basic designated initialization too, the bad news is that it will only be a very limited subset of the C99 feature
1) use -Wall, -Werror, -W4.
I suggest -pedantic as well; surely you were already using the first two?
2) designed initializers are nice
Indeed, they are.
3) you don't need RAII because it's only for memory
False. C++ is my main language, and we use RAII in many ways: callback invocation, taking locks, pinning cache items, timing segments of code, etc etc.
4) small allocations are bad
Small allocations are indeed highly inefficient, but this can be addressed using arena allocation. This works because it's actually the deallocations and associated defragmentation that are expensive.
5) ...so the solution for memory management is not to have structs that contain pointers (?)...
You should use the simplest data model possible, but only the simplest data models don't require having pointers in structs. For example, any recursive structure would have to have a pointer.
6) and all your objects should be allocated from object specific free lists
Having an object pool doesn't address memory leaks. You would still have to return the object to the pool at some point.
Once there, of course, the objectively more powerful, safer, faster (than can be coded in C) C++ libraries come into reach, and you are off to the races. If you have been deluded that C++ is all about virtual functions and heap allocations, you are in for a happy surprise. (My programs run for days or weeks using neither.) The most distinctive development in C++ in the past decade is that it is overwhelmingly more fun to code in, than before, and than C.
Different people trace the fun to different places, but for me it centers in generic lambdas. I like to use them to invert control structures and cut down repetitive code.