Hi, I'm Tomasz
I'm a professional C++ software engineer with over a decade of hands on development experience with variety of technologies (mostly Linux & embedded systems). This is my blog.
Find me on social media
This is a short overview of things added along with c11 and c23 which I find useful or interesting.
auto keyword… yet again auto has been repurposed in C23. Originally, it defined a storage duration for local variables (similarly as static) now, comparably as in C++, it can be used for type inference purposes.
1 2 3 4 5 6 7 void foo() { // auto is implied int i = 123; // same thing, auto keyword is redundant auto int j = 123; } In its original purpose, variables marked with auto have their storage automatically allocated and deallocated on scope entry and exit.
c++20 introduced concepts to the standard thanks to which now, we can specify constraints and restrictions on template parameters that a given type, variable or a function template accepts. Similarly as with e.g. virtual classes defining interfaces for a family of types through inheritance, concepts allow creating interfaces for generic code. With concepts, just by looking at the template declaration we know what to expect and what types are accepted. Having a simple template like:
There’s one repeating pattern in all C++ code bases I’ve worked with. shared_ptr is abused - one way or another. There are many reasons, sometimes people just start with shared_ptr instead of unique_ptr out of laziness, sometimes it’s the sole, default smart pointer they rely on. Often, it’s a result of many passes of refactoring and eventual quality degradation with time. This leads to all sorts of problems but there’s are definitely some repeating patterns.
Welcome to the third and concluding part of the series about dependency and package management in C++ projects in which I’m gonna mostly focus on solutions built into build systems themselves. If you haven’t already, I encourage you to have a quick read of the first and the second part.
Overview In this part I’ll have a closer look on meson wraps, cmake’s FetchContent, bazel’s central registry and, a bit unconventionally, conda.
Recently, I’ve learned about constexpr virtual functions. The support for which was added along with C++20. C++23 additionally allows for constexpr new (with some restrictions). There’s a great article about constexpr virtual on C++ stories blog, followed by another great post about constexpr new. I highly recommend reading Bartek’s posts.
Wanting to add this new feature to my tool belt I was wondering what would be the best application for constexpr virtual and how would I use them in my code.
Introduction of concepts in c++20 brought along a new set of problems requiring solving related to wrapper types (like e.g. std::optional, std::variant).
Specifically, special functions (like constructors, copy constructors, destructors etc) need to have the same type of traits as the type they are wrapping i.e. these have to be copyable if the underlying type is copyable, trivially copyable if the underlying types are trivially copyable, and trivially destructible if the underlying types are trivially destructible.
Sure, = default is great and convenient especially for destructors and other special member functions but when declaring a class that’s meant to be a part of a shared library’s interface I try to avoid using it and here’s my reasons why.
TLDR Tip When writing an interface class, containing virtual functions, don’t use = default or include inline definitions in class’s header file to avoid vtable/symbols duplication across compilation targets that use the header.