This is a short one, but I just wanted to talk about how I am going to try and enjoy C++, or at least a subset of features from it.
Features I am using
- RAII (destructors): this is literally a life-saver, an angel sent from the sky to save programming. Jokes aside, RAII is very useful and one of the few reasons C++ is worth it.
- Basic templates: templates can become some real assholes but, if used simply, they are also a really useful feature C++ offers over C.
- Non-owning views:
std::span<T>for making an efficient view of vectors, arrays, etc. andstd::string_viewfor making an efficient non-owning view of a string. std::unique_ptrandstd::array: single ownership without manual free, and arrays know their size.- Namespaces and
enum class: they replacemylib_subthingy_prefixes by nicerMyLib::Subthingy::and avoid enum-name collisions, also life savers. constexprandauto:constexprreplaces a lot of macro constants and lookup-table generation.autosaves me from spelling out long typenames.- Lambdas: no more
callback + void* userdatawith captured state.
Features I am banning
- Inheritance and
virtual. - Exceptions (
-fno-exceptions). - RTTI (
-fno-rtti). - Operator overloading unless ultra necessary (math types).
iostream.<algorithm>/ranges pipelines.- Anything with SFINAE.
Drawbacks
- RAII drags in copy/move semantics: I have to learn move semantics mandatorily if I want to use C++, at some point I will need to decide what happens to a type when it’s copied or moved.
- Subset is self-enforced: every library, header and error will still use the full language. Some errors are still awful even if I limit my feature usage.
Conclusions
I am going to try and use C++ with a limited subset, seeing if I like it by making the same program in plain C and in this C++ subset.
At the end of the day, there are some C++ features I do enjoy.