Posts for: #Cpp

User-Defined Deduction Guides for Class Templates in C++

User-Defined Deduction Guides for Class Templates in C++

I remember when I first got serious about modern C++. It was in 2017, my company was starting a new project from scratch, and we chose the latest standard: C++17, which had just been released. Diving into all the modern features of C++, I remember one feature that left me particularly puzzled: user-defined deduction guides. They are used to influence CTAD (Class Template Argument Deduction), which was a major feature of C++17. I was puzzled because I couldn’t understand why anyone would ever need to write such a guide by hand…

[Read more...]

clang-tidy, const & (N)RVO

Almost one year ago, clang-tidy threw an unexpected and unfamiliar message in my face: warning: constness of 'str' prevents automatic move [performance-no-automatic-move] (documented here). The incriminated code looked something like this:

auto build_path(const std::string& basedir, int index) {
	const auto str = fmt::format("{}/output/{}", basedir, index);
	
	fmt::print("{}\n", str);
	// ... do some work with 'str'...
	
	return str; // <-- the warning was here
}

I didn’t have time to investigate, so I wrote down a note, swore I would look into it later, and just forgot… Until today. I thought it would be simple. It was not. Because, as always, C++ is full of surprises.

[Read more...]

CMake on SMT32 | Episode 8: building with Docker

CMake on SMT32 | Episode 8: building with Docker

In this episode, we will use Docker to set up and manage our build environment.

The embedded world doesn’t always embrace the latest software development techniques. I acknowledged this in the previous episode about unit tests. Four years ago, when I started this series, I was totally unaware of Docker for instance. Since then, I have used it for non-embedded projects and I think it could have solved some issues I encountered in the past. I would like to share my experience and demonstrate that it can perfectly fit in an MCU project.

[Read more...]

CMake on SMT32 | Episode 7: unit tests

CMake on SMT32 | Episode 7: unit tests

It’s been almost four years, and it’s about time to revive this series on CMake and STM32 (or MCUs in general, STM32 just serves as a tangible example).

Funny enough, I’m not working with microcontrollers anymore…. This is a very recent change in my professional career, even if I think I will probably work again with MCUs sooner or later. However, I still use CMake to build C++ projects! The real reason to resume this series is my desire to write an article Docker for builds. In 2020, I had never used Docker at all. Today, I use it very often, and I’m convinced it could have solved some build and environment issues back then.

[Read more...]

vtables under the surface | Episode 3 - How virtual functions are actually called

In this episode, we will see how invoking a virtual function in C++ translates into assembly instructions. We will see how our class instance is constructed and how it relates to the vtable. Then, we will see how this vtable is used to call the appropriate function.

In you have actually built the project and analyzed the binary in the previous episode, don’t forget to remove the -fno-rtti option and to rebuild the project. I will use this binary as a reference here.

[Read more...]