If you pick a random talk at a C++ conference these days, there is a fair chance that the speaker will mention safety at least a couple of times. It’s probably fine like that. The committee and the community must think about improving both the safety situation and the reputation of C++. If you follow what’s going on in this space, you are probably aware that people have different perspectives on safety. I think almost everybody finds it important, but they would solve the problem in their...| Sandor Dargo’s Blog
I have learned, written and spoken a lot about the Standard Template Library during the course of the last years. My sources have been mostly websites such as cppreference.com, cplusplus.com, blogs, youtube videos, but not so many books.| Sandor Dargo’s Blog
This is a statement frequently repeated by people who either just more familiar with the header in C++ and/or are advocates of functional programming in C++. And of course, let’s not forget about the people who just repeat what others say without understanding the reasons behind.| Sandor Dargo’s Blog
Since constexpr was added to the language in C++11, its scope has been gradually expanded. In the beginning, we couldn’t even use if, else or loops, which were changed in C++14. C++17 added support for constexpr lambdas. C++20 added the ability to use allocation and use std::vector and std::string in constant expressions. In this article, let’s see how constexpr evolves with C++26. To be more punctual, let’s see what language features become more constexpr-friendly. We’ll discuss libr...| Sandor Dargo’s Blog
To my greatest satisfaction, I’ve recently joined a new project. I started to read through the codebase before joining and at that stage, whenever I saw a possibility for a minor improvement, I raised a tiny pull request. One of my pet peeves is rooted in Sean Parent’s 2013 talk at GoingNative, Seasoning C++ where he advocated for no raw loops. When I saw this loop, I started to think about how to replace it: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30...| Sandor Dargo’s Blog
Up until C++23, functions, classes, function and class templates could be declared as friends. Starting from C++26, thanks to Jody Hagins’ and Arthur O’Dwyer’s proposal, P2893R3, friendship can also be granted to a pack of types. Who does a pack of friends look like? In earlier standards, we must declare friend class templates one by one, just like in the example below. 1 2 3 4 5 6 template class Foo { friend T; friend U; }; Note the default values for the template parameters. That make...| Sandor Dargo’s Blog
C++11 introduced parameter packs to provide a safer way to pass an undefined number of parameters to functions instead of relying on variadic functions. While packs are a useful feature, and since C++17 it’s so easy to use them in fold expressions, extracting a specific element of a pack is somewhat cumbersome. You either have to rely on some standard functions not made for the purpose or use “awkward boolean expression crafting or recursive templates”. None of them is unbearable, but i...| Sandor Dargo’s Blog
Last week, between the 3rd and 5th of July, I had the privilege to attend and present at C++ on Sea 2024 for the 5th time in a row! I’m grateful that the organizers accepted me not simply as a speaker, but they granted me a double slot to deliver a half-day workshop about how to reduce the size of a binary. I’m also thankful for my management that they gave me the time to go to Folkestone and share our knowledge on binary size. Last but not least, a great thanks goes to my wife, who took ...| Sandor Dargo’s Blog
If at the end of a conference talk, I cannot answer a question and there is nobody to my rescue, I offer to reply later in the form of a blog post. At C++ on Sea, someone asked me about the implications of dynamic linking concerning binary size. I hope I remember the question well! Let me phrase it differently: assuming the same code, what if you deliver an executable where libraries are dynamically linked and what if they are statically linked? How much bigger the dynamic version will be ove...| Sandor Dargo’s Blog
These months, I try to better understand how our code affects binary sizes. Last week, we had a look into storage durations and memory allocations. This week, let’s have a look into special member functions. We are going to discuss whether it matters or not if we make our special functions default or if we provide empty implementations. We are also going to see if we should have the implementations in the header or in the cpp files. Or maybe not having them all is the best? The simplest cas...| Sandor Dargo’s Blog
Today, we are going to talk about when and why structs should have constructors if they should have them at all. We are also going to see once again that generic best practices and best practices to reduce binary size do not always go hand in hand. I had some time to dedicate to cleaning up code. I remembered that recently I saw some structs like this: 1 2 3 4 SomeSimpleStuct data; data.a_number = 42; data.a_character = 'a'; return data; I didn’t like this code. Even though the struct didn...| Sandor Dargo’s Blog
Have you heard about std::ref and std::cref? The helper functions that generate objects of type std::reference_wrapper? The answer is probably yes. In that case, this article is probably not for you. But if you haven’t heard about them, or the only usage of std::reference_wrapper you faced was storing references in a vector, then probably it’s worth reading on. This article is inspired by some failing tests that needed me to use std::ref in order to pass them. What does reference_wrapper ...| Sandor Dargo’s Blog
Arne Mertz talked about misused guidelines at C++OnSea. Among those, there was the rule of 5. Which made me think about a pattern I’ve seen. Let’s first repeat what the rule of 5 says. The Rule of Five tells us that if we need to define any of a copy constructor, copy assignment operator, move constructor, move assignment operator or destructor then we usually need to define all five. Fair enough. Have you ever seen classes where the default constructor and destructor are explicitly defau...| Sandor Dargo’s Blog
Do we need a default constructor? What does it mean to have a default constructor? What happens if we don’t have one? Those are the questions we are going after in this article. A default constructor is a constructor that takes no arguments and initializes - hopefully - all the members with some default values. If you define no constructors at all, it’ll even be generated for you. Do we need default constructors? It really depends. Let’s first approach this question from a design point ...| Sandor Dargo’s Blog
During the last few months, we discussed a lot about how to write code to limit the size of our binary. We should think twice if we want to turn a class into polymorphic, exceptions also take a heavy toll and we might end up defaulting special functions in the cpp file which is quite unintuitive. There are practices going against common sense and also that can be considered a best practice in any case. I think it’s good to know about them. But how you compile your code is also important! So...| Sandor Dargo’s Blog