<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sandor Dargo</title>
    <description>The latest articles on DEV Community by Sandor Dargo (@sandordargo).</description>
    <link>https://dev.to/sandordargo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F37458%2Fadbc8839-4e49-4a14-bb5d-7a70ff635d1f.jpg</url>
      <title>DEV Community: Sandor Dargo</title>
      <link>https://dev.to/sandordargo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandordargo"/>
    <language>en</language>
    <item>
      <title>My way to get into conferences</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 28 Jun 2023 06:15:59 +0000</pubDate>
      <link>https://dev.to/sandordargo/my-way-to-get-into-conferences-36bg</link>
      <guid>https://dev.to/sandordargo/my-way-to-get-into-conferences-36bg</guid>
      <description>&lt;p&gt;In this article, I'll share with you my journey to become a "regular" conference speaker. I'll also explain what regular means in my case. Don't expect any magic from the article, I cannot offer you anything that would work in the short term. It took me years to get where I am, but I'm sure that if getting into conferences is a major goal for you, you can achieve it faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Going to&lt;/em&gt; or &lt;em&gt;speaking at&lt;/em&gt; conferences?
&lt;/h2&gt;

&lt;p&gt;A few years ago, after having completed most of the internal trainings at my job, I started to see conferences both as a perk and a replacement for regular trainings. I thought that conferences are a great way to learn about trends, technologies, and methodologies. I wanted to go to conferences and not only to smaller local events. One of my former bosses who could go to &lt;a href="https://cppcon.org/"&gt;CppCon&lt;/a&gt; told me that he'd help me if I asked for his help. So did I.&lt;/p&gt;

&lt;p&gt;His boss also helped me.&lt;/p&gt;

&lt;p&gt;I remember that a Friday afternoon my senior manager rushed to my desk really excited saying there are very few people who could go that year due to budget cuts, but I'll be one of them. Expect the mail! The mail arrived and I didn't understand it.&lt;/p&gt;

&lt;p&gt;We were talking about a week in Colorado, but the mail was about a one-day conference in Paris. Was it in addition? Was it by mistake?&lt;/p&gt;

&lt;p&gt;No. It was a budget cut.&lt;/p&gt;

&lt;p&gt;There were two outcomes of it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I had a great time at &lt;a href="https://www.sandordargo.com/blog/2019/06/26/travel-report-cppp"&gt;CPPP, where my trip report&lt;/a&gt; was among the two best ones according to the organizers.&lt;/li&gt;
&lt;li&gt;I understood something I had been already suspecting. If I wanted to go to a conference without paying for it, I rather go as a speaker. So I doubled down on my efforts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to prove that you're ready to speak?
&lt;/h2&gt;

&lt;p&gt;I had two problems at that point. I had no credibility as a speaker and in addition, I had nothing to speak about.&lt;/p&gt;

&lt;p&gt;Not an optimal situation when you want to speak at conferences.&lt;/p&gt;

&lt;p&gt;In this section, I'll delve into the first problem. A bit earlier, in 2017, &lt;a href="https://www.sandordargo.com/blog/2017/05/16/attending-my-first-it-conference"&gt;I attended my first IT conference, RivieraDev&lt;/a&gt;. Each year, &lt;a href="https://rivieradev.fr/"&gt;RivieraDev&lt;/a&gt; has a special track called &lt;em&gt;"we are not just coders"&lt;/em&gt;. On this track, you'll find presentations both by non-dev people (usually there are 1-2 gastronomic ones) and non-IT-related presentations by developers. You might learn about multi-day hikes with very light backpacks or about lockpicking.&lt;/p&gt;

&lt;p&gt;There I realized that you don't have to be "special" to talk at a conference.&lt;/p&gt;

&lt;p&gt;This track gives you a nice opportunity to speak at a conference and get some real-life experience. The next time you apply, you can mention that as a previous experience at a conference. Even though many conferences specifically mention that they welcome new speakers, experience doesn't hurt.&lt;/p&gt;

&lt;p&gt;I only needed some compelling topics. So I spoke about things that not everyone does (well) and something I had been passionate about.&lt;/p&gt;

&lt;p&gt;In my first year, I spoke about how to organize your holidays. The second year I spoke about sourdough baking where I also brought some freshly baked bread and pastries.&lt;/p&gt;

&lt;p&gt;I had some speaking engagements that I could mention in my applications and I definitely proved to myself that I was ready.&lt;/p&gt;

&lt;p&gt;At that point, I needed good topics.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes a good topic?
&lt;/h2&gt;

&lt;p&gt;I think we can agree that holiday organisation and sourdough baking are interesting and fun, but probably not the topics you'd call compelling at a developer conference.&lt;/p&gt;

&lt;p&gt;Ideally, you should find a topic that meets 3 requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;would be accepted by the organizers / attendees&lt;/li&gt;
&lt;li&gt;something you will be able to present&lt;/li&gt;
&lt;li&gt;something that interests you&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first requirement is obvious. If the organizers wouldn't accept it, if nobody would come to your talk, then you have no talk.&lt;/p&gt;

&lt;p&gt;At the same time, an accepted submission is worth nothing if you won't be able to present at the conference. It will even ruin your credibility. Mind the future tense. &lt;strong&gt;I don't think you have to be able to present a topic at the moment of submission.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I don't mean that you don't have the slides ready. Of course, you don't. I mean that you know close to nothing about a topic. In my opinion, it's not a problem. At the moment, you only have to know enough to write a compelling abstract and outline. By being able to do so, you probably understand if that topic isn't too complex for the current you. &lt;/p&gt;

&lt;p&gt;I knew close to nothing about &lt;a href="https://youtu.be/BEmAo6Fdg-Q"&gt;undefined behaviour&lt;/a&gt; when I submitted a proposal in 2020. I also knew that I shouldn't submit anything about &lt;a href="https://www.sandordargo.com/blog/2019/06/26/travel-report-cppp"&gt;SFINAE&lt;/a&gt; because I was (and am) clearly not there yet to talk about it. To meet this criteria you need some humility.&lt;/p&gt;

&lt;p&gt;You might think that I'm crazy, but this is an extremely motivating way to learn about a topic. As such I have a very hard deadline to finish my research, I have a reason that kicks me out of bed every single morning earlier than the rest of my family.&lt;/p&gt;

&lt;p&gt;It's not enough that you have a topic you will be able to speak about and that it would be acceptable to the organizers. That topic must be interesting for you. You'll have to prepare a lot. Maybe a little bit every day. You'll have to speak about it to others and most probably not only during your talk but during coffee breaks, at breakfast, at the pub, etc. Talk about something that you like, something that you find interesting. It'll be better for you and for the attendees too.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to know what the organizers want?
&lt;/h2&gt;

&lt;p&gt;The second and third criteria are only up to you. But what about the first one? How could you know what the programme committee would like?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"I cannot read others' minds!"&lt;/em&gt; - you might object. That's right, but you can do your homework!&lt;/p&gt;

&lt;p&gt;You can read the programs of earlier years and of other similar conferences. You can study what were topics that got accepted, what were popular topics and what topics people barely spoke about. You can more or less understand which conferences like to have some talks on softer topics such as &lt;a href="https://cpponsea.uk/2023/sessions/why-clean-code-is-not-the-norm.html"&gt;clean code&lt;/a&gt;, and where that most probably wouldn't be accepted. You might also identify certain patterns for abstracts that people use.&lt;/p&gt;

&lt;p&gt;You have to accept that especially as a fresh speaker you'll have to face lots of rejections. That's fine. Maybe you won't even get into any conference the first year. That's fine. However, there is a fair chance that you'll be invited to program committees where you'll have the chance to review other submissions. That's a great opportunity to learn about trending topics and the quality of submitted proposals.&lt;/p&gt;

&lt;p&gt;Remember, when you get into a program committee, don't only review others' topics, but also analyze them! What do they want to talk about? How deep, how well structured their abstracts are? What kind of comments do they get from other reviewers? Take some notes and compare them later with the line-ups to understand where the bar is and draw your conclusions for the next year.&lt;/p&gt;

&lt;p&gt;On certain evaluation platforms, you'll get feedback. Take them into account. Maybe you can improve your submission for next year.&lt;/p&gt;

&lt;p&gt;If you really care about the learning opportunity, find user groups or local events that would give you an opportunity to present your ideas. You'll learn a lot, you might engage in deep technical discussions with experts and you can improve your content so that next year you might come back with an enhanced proposal that has higher chances to get accepted.&lt;/p&gt;

&lt;h2&gt;
  
  
  But does this actually work?
&lt;/h2&gt;

&lt;p&gt;For me, it worked and I'm more than grateful both to the organizers and my employers for the several opportunities they granted me. I have to mention my employers too because while it's often difficult to get money to travel and to buy a conference ticket, it's much easier to convince your bosses to give you the time to speak at a conference.&lt;/p&gt;

&lt;p&gt;In the beginning, I applied for almost anything locally. I wanted to get experience and build up credibility. As I mentioned I talked about sourdough baking and travel organization at RivieraDev. My first breakthrough was in 2019 when I got the chance to talk at an international conference 200 km away from my home, at &lt;a href="https://www.sandordargo.com/blog/2020/01/15/i-went-to-devops-d-day-2019"&gt;DevOps D-Day in Marseille&lt;/a&gt;, I talked about Coding guidelines and decision fatigue. An important topic to me.&lt;/p&gt;

&lt;p&gt;2020 was a big shock for most of us. I got accepted for &lt;a href="https://cpponsea.uk/"&gt;C++ On Sea&lt;/a&gt;, the first time for a C++ conference. No, that's what shocked most of us. It was that Covid came,  restrictions came and I stayed home. Nevertheless, the conference was held online and it still was a big experience.&lt;/p&gt;

&lt;p&gt;In the coming one and a half years, I spoke at several online events, at CPPP, MeetingC++, CppNow, CppCon... I presented probably about 4 different topics. And in 2022, finally I got to an in-person C++ Conference, C++ On Sea 2022 and this year (in 2023) once again I can go to C++ On Sea.&lt;/p&gt;

&lt;p&gt;Thanks to the restrictions I had several opportunities. I needed less time and no travel budget. Since most conferences are luckily in-person once again, I apply for less. But that's another story. My point is that what I explained above really worked for me and I'm sure that if that's what you're looking for you can use the above pieces of advice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, I covered one way that can help you get closer not only to attend but to speak at conferences. In my opinion, this is way better than just attending, because often (most of) your costs will be covered, you'll learn way more and build more connections. Not to mention that it's easier to convince your boss to let you speak at a conference than to buy you a ticket.&lt;/p&gt;

&lt;p&gt;If you start from zero, make sure that you practice enough and that you build up some credibility by speaking at smaller events. Even if you give some non-technical talks. Once you're confident and you have some experience, start applying for bigger events with more technical talks. Grab all the opportunities to study successful proposals and use the findings to make yours better.&lt;/p&gt;

&lt;p&gt;Once you got accepted, make sure that you prepare well and you live up to the expectations and you don't waste your time. But that's another story.&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>watercooler</category>
      <category>conference</category>
      <category>publicspeaking</category>
      <category>perseverance</category>
    </item>
    <item>
      <title>The 3 best books to level up your C++</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 21 Jun 2023 07:58:10 +0000</pubDate>
      <link>https://dev.to/sandordargo/the-3-best-books-to-level-up-your-c-3ifm</link>
      <guid>https://dev.to/sandordargo/the-3-best-books-to-level-up-your-c-3ifm</guid>
      <description>&lt;p&gt;In this article, I'll present you 3 books that can level up your C++. For this article, I'm assuming that you have little or medium experience with C++ and you're looking for ways to reach the next level.&lt;/p&gt;

&lt;p&gt;Reading blogs is also a good way to enhance your knowledge and keep up with the news. &lt;a href="https://github.com/sandordargo/cpp-resources/blob/master/blogs.md"&gt;Here is my curated list of C++ blogs.&lt;/a&gt; There are many ways and you should not stick only to one type of source.&lt;/p&gt;

&lt;p&gt;In this article, I'm focusing on books. On books that will help you reach the next level. With the level of knowledge I assume you have, you should be familiar with the language and library basics, maybe a little bit more. You should be able to use them with confidence and probably understand how they work, and some of their internals.&lt;/p&gt;

&lt;p&gt;With the below 3 books, you'd learn the ins and outs of several C++ best practices, you'd learn a lot about templates to make them less dreadful and of course, one must get comfortable with architecture if he/she wants to make some career progression.&lt;/p&gt;

&lt;p&gt;I'm not going to share full reviews of these 3 books, but you'll get the main idea behind them and a link to my full reviews on them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beautiful C++ by J. Guy Davidson and Kate Gregory
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.sandordargo.com/blog/2022/04/16/beautiful-cpp-by-kate-gregory-and-guy-davidson"&gt;Beautiful C++: 30 Core Guidelines for Writing Clean, Safe and Fast Code by J. Guy Davidson and Kate Gregory&lt;/a&gt; is the book to read if you want to learn how to write more readable and therefore more maintainable C++ code. In it, you’ll find 30 handpicked guidelines from the &lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines"&gt;Core Guidelines&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The authors explain each of those guidelines in detail. By reading the book, you will understand how and why you should apply them. While it doesn't cover all the guidelines (that would be a really long book!), it introduces the &lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines"&gt;Core Guidelines&lt;/a&gt; to you and probably you'll open it up from time to time to see what is in there.&lt;/p&gt;

&lt;p&gt;Among others, by reading &lt;a href="https://www.sandordargo.com/blog/2022/04/16/beautiful-cpp-by-kate-gregory-and-guy-davidson"&gt;this book&lt;/a&gt; you'll learn why it's sane to avoid trivial getter and setters, why you should specify concepts or why you should opt for immutable data.&lt;/p&gt;

&lt;p&gt;You'll also learn how to write more beautiful and more maintainable code than most of your fellow developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Software Design by Klaus Iglberger
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.sandordargo.com/blog/2022/12/17/cpp-software-design-by-klaus-iglberger"&gt;C++ Software Design by Klaus Iglberger&lt;/a&gt; is a great book that every non-beginner C++ developer should read in order to level-up their knowledge. Reading this book, you'll learn about the most useful design patterns in C++. More than that, you'll not only learn about their classical implementation, which is usually based on dynamic polymorphism, but you'll also learn about modern implementation approaches. The latter ones are usually value-based and let the compiler do most of the work, instead of the run-time. You also get comparisons, pros and cons so that you can choose in your real-life projects, which implementations you should consider.&lt;/p&gt;

&lt;p&gt;If you ever wanted to understand (better) the strategy/policy or the visitor patterns, if you ever wondered what type erasure is or if you wanted to understand why there are so many debates about the singleton pattern, you should read this great book from &lt;a href="https://www.sandordargo.com/blog/2022/12/17/cpp-software-design-by-klaus-iglberger"&gt;Klaus Iglberger&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Template Metaprogramming with C++ by Marius Bancila
&lt;/h2&gt;

&lt;p&gt;Templates can be intimidating. Even though every C++ developer is using them at least through standard library types, more elaborated usages can be seen difficult. In fact, they are. &lt;a href="https://www.sandordargo.com/blog/2021/06/02/different-ways-to-achieve-SFINAE"&gt;SFINAE is not easy to grasp&lt;/a&gt; and if you talk to developers who haven't kept up that much with the evolution of C++, they will strengthen this feeling in you.&lt;/p&gt;

&lt;p&gt;But if you understand the basics well and use modern language features, such as &lt;a href="https://www.sandordargo.com/blog/2022/06/15/cpp23-narrowing-contextual-conversions-to-bool"&gt;&lt;code&gt;if constexpr&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://leanpub.com/cppconcepts"&gt;concepts&lt;/a&gt;, templates become much easier. &lt;a href="https://www.sandordargo.com/blog/2022/10/28/template-metaprogramming-with-cpp-by-marius-bancila"&gt;Template Metaprogramming with C++ by Marius Bancila&lt;/a&gt; provides you with exactly that. &lt;/p&gt;

&lt;p&gt;It has a clear structure and builds from the simple to the complex. It starts with the basics of templates, not forgetting about the terminology, history, and why you’d use templates in the first place. In the second part, it moves to advanced template concepts. It details how overload resolution works with templates, how to use &lt;em&gt;forwarding/universal references&lt;/em&gt;, what is &lt;em&gt;CTAD&lt;/em&gt;, and how to use &lt;code&gt;decltype&lt;/code&gt; and &lt;code&gt;std::declval&lt;/code&gt;. It also covers the already mentioned &lt;em&gt;SFINAE&lt;/em&gt; and how to replace them with modern techniques.&lt;/p&gt;

&lt;p&gt;In the third and final part, Marius Bancila details several design and implementation patterns that are using templates. The &lt;a href="https://www.sandordargo.com/blog/2019/03/13/the-curiously-recurring-templatep-pattern-CRTP"&gt;Curiously Recurring Template Pattern&lt;/a&gt;, mixins, and type erasure are well explained with lots of code examples. The book also gives a nice introduction to the ranges library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, you could learn about 3 great books that will help you reach the next levels in C++. With &lt;a href="https://www.sandordargo.com/blog/2022/04/16/beautiful-cpp-by-kate-gregory-and-guy-davidson"&gt;Beautiful C++&lt;/a&gt;, you'll learn how to write more beautiful and more maintainable code than most of your fellow developers. &lt;a href="https://www.sandordargo.com/blog/2022/12/17/cpp-software-design-by-klaus-iglberger"&gt;C++ Software Design by Klaus Iglberger&lt;/a&gt; will teach you about the most important design patterns in C++ and how their pointer and value-based implementations compare against each other. Last, but not least, by reading &lt;a href="https://www.amazon.com/dp/1803243457/?&amp;amp;_encoding=UTF8&amp;amp;tag=sandordargo-20&amp;amp;linkCode=ur2&amp;amp;linkId=3b52fe7dec703403826e4dab46d22da9&amp;amp;camp=1789&amp;amp;creative=9325"&gt;Template Metaprogramming by Marius Bancila&lt;/a&gt;, you'll learn the most important details of how to deal with templates, including modern features such as &lt;a href="https://leanpub.com/cppconcepts/"&gt;concepts&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>books</category>
      <category>selfimprovement</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Fold expressions in C++</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 14 Jun 2023 18:02:07 +0000</pubDate>
      <link>https://dev.to/sandordargo/fold-expressions-in-c-54le</link>
      <guid>https://dev.to/sandordargo/fold-expressions-in-c-54le</guid>
      <description>&lt;p&gt;In this article, we are going to talk about time expressions. First, we are going to take some time to define what they are in case you're not familiar with them. We'll do this using C++ syntax, but we must keep in time that the concept is not exclusive to our favourite language.&lt;/p&gt;

&lt;p&gt;Then we are going to see what the standard library offers us in terms of algorithms and we'll also see how they evolved so far since they were introduced in C++17 and what new functions we are going to get with C++23.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are fold expressions?
&lt;/h2&gt;

&lt;p&gt;When you have several values of the same type and you'd like to apply a binary function to each of these values and produce one single result then you essentially want to fold or reduce them. When you sum up several values and return the result, then you use might use a fold expression.&lt;/p&gt;

&lt;p&gt;Using a fold expression lets you &lt;a href="https://www.sandordargo.com/blog/2020/05/13/loops-vs-algorithms"&gt;not use an ugly loop&lt;/a&gt;, it gives you a concise way to perform any binary operation and give back a single result.&lt;/p&gt;

&lt;p&gt;We differentiate between left and right fold operations depending on from which direction we apply the operation.&lt;/p&gt;

&lt;p&gt;When we start from the left, we talk about left folding and when we start from the right, we talk about right folding.&lt;/p&gt;

&lt;p&gt;Using the example of summing up 5 numbers, let's represent the difference between left and right folding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="c1"&gt;// equivalent to left folding&lt;/span&gt;
&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// equivalent to right folding&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the direction doesn't matter if an operation is associative, in other cases, it's important, such as for division.&lt;/p&gt;

&lt;p&gt;A fold expression can be also called &lt;em&gt;parameter pack expansion&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The syntax in C++
&lt;/h2&gt;

&lt;p&gt;There are two ways to approach fold expressions in C++. The first is through the syntax supporting writing fold expression and we can use fold expressions thanks to algorithms in the standard library.&lt;/p&gt;

&lt;p&gt;In this section, let's talk about the syntax that was introduced in C++17.&lt;/p&gt;

&lt;p&gt;The fold expression syntax (&lt;code&gt;...&lt;/code&gt;) can be used only with &lt;a href="https://www.sandordargo.com/blog/2023/05/03/variadic-functions-vs-variadic-templates"&gt;variadic templates???&lt;/a&gt;, they cannot be directly used on standard containers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="nc"&gt;Ts&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ts&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, there are three items to notice.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the template parameter list there, there are three dots after the &lt;code&gt;typename&lt;/code&gt; keyword, indicating that there can be more than one item of the same type&lt;/li&gt;
&lt;li&gt;In the function parameter list, there are also three dots, but after the actual type indicating also that there can be many of them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You could read about the details of those in &lt;a href="https://www.sandordargo.com/blog/2023/05/03/variadic-functions-vs-variadic-templates"&gt;this article on variadic templates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The third thing to notice is the &lt;code&gt;(numbers + ...)&lt;/code&gt; expression. That is called the fold expression and this particular one is a unary right fold. It will be expanded as &lt;code&gt;(1 + (2 + (3 + 4)))&lt;/code&gt;. The parameter pack you have on the left will be "combined" by using the operator in the middle. If the fold expression would have started with the three dots (&lt;code&gt;(... + numbers)&lt;/code&gt;) then we would talk about a unary left fold expression expanded as &lt;code&gt;(((1 + 2) + 3) + 4)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are a couple of more things here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why did I use the &lt;em&gt;unary&lt;/em&gt; word?&lt;/li&gt;
&lt;li&gt;Can we pass in a standard container?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I used the unary keyword because the above is not the only type of fold expression that exists. Binary fold expressions are also a thing. The main difference is that with binary fold expressions, you define an initial value so that folding doesn't start by combining the first two elements of the variable pack, but by combining the first item with the initial value.&lt;/p&gt;

&lt;p&gt;If we rewrite a unary fold to a binary fold by using the identity element of a given type/operation combination, then we can be sure to have a well-defined and meaningful behaviour even if the variable pack does not contain any items. Binary fold expression can also start from the left (&lt;code&gt;(0 + ... + numbers)&lt;/code&gt;) or from the right (&lt;code&gt;(numbers + ... + 0)&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;For standard containers, we'll see how to use the standard algorithms. We could pass a container to the above &lt;code&gt;sum()&lt;/code&gt; function but it wouldn't sum up the &lt;code&gt;vector&lt;/code&gt;. The template would be expanded into a function that takes one &lt;code&gt;std::vector&lt;/code&gt; as &lt;code&gt;Ts&lt;/code&gt;. But it could effectively use the &lt;code&gt;operator+&lt;/code&gt; on different vectors combining them into one &lt;code&gt;std::vector&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fold algorithms
&lt;/h2&gt;

&lt;p&gt;While fold expressions were only introduced in C++17, we already had fold algorithms earlier in the standard. The most well-known is &lt;code&gt;std::accumulate&lt;/code&gt; which was added in C++11. &lt;code&gt;std::reduce&lt;/code&gt; is another fold operation added by C++17.&lt;/p&gt;

&lt;p&gt;With C++23, we get 6(!) other fold operations, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_left&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_left_first&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_right&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_right_last&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_left_with_iter&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::ranges::fold_left_first_with_iter&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;std::accumulate&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;std::accumulate&lt;/code&gt; takes 4 parameters. The first two define the range of elements it should fold. It also takes an initial value for the folding operation and as a last parameter, it takes a binary operation that it will apply on the running accumulated value (starting with the initial value) and the next item of the input range going from left to right. The binary operation defaults to &lt;code&gt;operator+&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;numeric&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"sum: "&lt;/span&gt; 
              &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="p"&gt;})&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
sum: 10
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned above, &lt;code&gt;std::accumulate&lt;/code&gt; performs a left fold. In order to apply a right fold, we have to pass in the input range in reverse order.&lt;/p&gt;

&lt;p&gt;It's also worth noting that we can fold a range of items into a type that is different from the initial value. So for example we can accumulate a range of &lt;code&gt;int&lt;/code&gt;s to a single &lt;code&gt;string&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;numeric&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;  &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;previousResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'-'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                  &lt;span class="p"&gt;})&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
1-2-3-4
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to avoid having an additional initial value and fold items starting right with the first item, just use the first item as an initial value and leave it out of the range you pass in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;numeric&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;  &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;previousResult&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'-'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                  &lt;span class="p"&gt;})&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
1-2-3-4
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;std::reduce&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;std::reduce&lt;/code&gt; was introduced later than &lt;code&gt;std::accumulate&lt;/code&gt;, only in C++17. While &lt;code&gt;std::accumulate&lt;/code&gt; is a left-fold operation, &lt;code&gt;std::reduce&lt;/code&gt; doesn't guarantee any order, it can take pairs of items in just any order. As such, its binary operation can only operate on elements that are at least convertible into the value type of the range. In other words, you cannot fold a container of integers into a string as an integer is not implicitly convertible to a string.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;std::reduce&lt;/code&gt; can be parallelized, and accordingly, it takes an optional &lt;code&gt;ExecutionPolicy&lt;/code&gt; parameter before all the others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;numeric&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
10
10
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The initial value is missing from the parameters of &lt;code&gt;std::reduce&lt;/code&gt; as it can default it to the default constructed value of the passed-in range's value type. This might be dangerous because the default constructed type might not always be the identity value.&lt;/p&gt;

&lt;p&gt;As &lt;code&gt;reduce&lt;/code&gt; might take items in just any order, the results might differ from the result of a left-fold operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;numeric&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;minus&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;minus&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;execution&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;minus&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;execution&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unseq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;minus&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"======&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;execution&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;par&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
1
25
25
1
======
16 8
4 2
8 2
32 6
26 1
25
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;accumulate&lt;/code&gt; we get &lt;code&gt;1&lt;/code&gt; as expected, but &lt;code&gt;reduce&lt;/code&gt; produces different outputs except for with the &lt;code&gt;unsequenced_policy&lt;/code&gt;. The last call, where we pass in a lambda doing an identical operation compared to &lt;code&gt;std::minus&lt;/code&gt;, reveals the reason. Subtraction is not commutative and associative, therefore when the items are evaluated in a different order, you won't have the same result.&lt;/p&gt;

&lt;p&gt;So when you make a decision between &lt;code&gt;accumulate&lt;/code&gt; and &lt;code&gt;reduce&lt;/code&gt;, you have to take into account that as well.&lt;/p&gt;

&lt;p&gt;Now let's move on to algorithms introduced by C++23.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;std::ranges::fold_left&lt;/code&gt; and &lt;code&gt;std::ranges::fold_right&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The names of these algorithms are straightforward. They either perform a left- or a right-fold on the passed-in range. As they are part of the &lt;em&gt;ranges&lt;/em&gt; library they can directly take a range (such as a container), but they also have overloads to take a range defined by its beginning and the end.&lt;/p&gt;

&lt;p&gt;Both the initial value and the binary operation must be provided.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
10
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we saw with &lt;code&gt;std::accumulate&lt;/code&gt;, these new fold algorithms can also fold items of a range into a different type, such as &lt;code&gt;int&lt;/code&gt;s to a &lt;code&gt;string&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
1-2-3-4
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;std::ranges::fold_left_first&lt;/code&gt; and &lt;code&gt;std::ranges::fold_right_last&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;It might happen that you don't have an identity element for the type you're folding. In those cases, you can start folding starting from the second item and use the first as the initial value. We already saw earlier how to do it with &lt;code&gt;std::accumulate&lt;/code&gt; when we passed &lt;code&gt;numbers[0]&lt;/code&gt; as the initial value and &lt;code&gt;numbers.begin()+1&lt;/code&gt; as the beginning of the input range.&lt;/p&gt;

&lt;p&gt;C++23 provides library support for those cases so you don't have to write error-prone code, you simply pass in the range, you skip the initial value and call the function that will take the first value from the range. There are 2 such functions so that we have support both for left and right folding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;optional&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left_first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;divides&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"folding has no value!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;reversedNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rbegin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rend&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_right_last&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reversedNums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"folding has no value!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's worth noting that in the second example we could not use &lt;code&gt;std::divides&lt;/code&gt;, because when we fold from the right to the left, we have to also change the order of the parameters for a non-commutative operation.&lt;/p&gt;

&lt;p&gt;Sadly, we cannot use these two functions when we want to fold a collection of elements into another type. We cannot fold a vector of integers into a &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;std::ranges::fold_left_with_iter&lt;/code&gt; and &lt;code&gt;std::ranges::fold_left_first_with_iter&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The previous functions returned only the folded value. These two functions return two values. In the first position, they return the iterator pointing at the end of the range and in the second position there comes the result. The main difference between these two functions is that &lt;code&gt;std::ranges::fold_left_with_iter&lt;/code&gt; takes an initial value and therefore it always returns a folded value, while &lt;code&gt;std::ranges::fold_left_first_with_iter&lt;/code&gt; doesn't take an initializer, uses the first item as the initial value and only returns a &lt;code&gt;std::optional&lt;/code&gt; as a value.&lt;/p&gt;

&lt;p&gt;Why are these functions needed?&lt;/p&gt;

&lt;p&gt;A range is not necessarily defined by two iterators. The first iterator denoting the beginning of a range is mandatory, but the second one can also be a sentinel, a condition defining when the iteration should finish.&lt;/p&gt;

&lt;p&gt;As the fold functions must compute the end, why not return them just in case?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;ranges&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;oneDigitNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;iota&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;take_while&lt;/span&gt;&lt;span class="p"&gt;([](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;});&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oneDigitNums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left_with_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oneDigitNums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Folding finished at "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" with the value "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;end2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fold_left_first_with_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oneDigitNums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Folding finished at "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;end2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" with the value "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
45
Folding finished at 10 with the value 45
Folding finished at 10 with the value 45
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we reviewed how fold expressions and functions evolved in C++. The first fold function (&lt;code&gt;std::accumulate&lt;/code&gt;) was introduced by C++11. Then C++17 also introduced &lt;code&gt;std::reduce&lt;/code&gt; and language support as well in the form of fold expressions to simplify folding for variadic templates.&lt;/p&gt;

&lt;p&gt;Last, but not least, we reviewed the 6 brand new fold functions C++23 is going to give us. We'll have the chance to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fold from both directions,&lt;/li&gt;
&lt;li&gt;use the first or last value as the initial value and &lt;/li&gt;
&lt;li&gt;we can even get back the iterator pointing just past the last value that was taken into account from a range.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>cpp23</category>
      <category>fold</category>
      <category>reduce</category>
    </item>
    <item>
      <title>Recent changes and personal commitments</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 07 Jun 2023 05:31:38 +0000</pubDate>
      <link>https://dev.to/sandordargo/recent-changes-and-personal-commitments-i9k</link>
      <guid>https://dev.to/sandordargo/recent-changes-and-personal-commitments-i9k</guid>
      <description>&lt;p&gt;Life is change. Such a cliché.&lt;/p&gt;

&lt;p&gt;Yet it is true. Everything is changing around us and if we try to stay the same, the world passes by. If you don't want to move backwards, you have to keep reinventing yourself.&lt;/p&gt;

&lt;p&gt;Even if I don't want to constantly reorganize my day, as a parent I must. The constraints set by the world, by the obligations of my kids make me do so.&lt;/p&gt;

&lt;p&gt;They are not the only reasons, but at least they have helped me to get into the habit of making changes. I'm not attached to how I do things.&lt;/p&gt;

&lt;h2&gt;
  
  
  The need for change
&lt;/h2&gt;

&lt;p&gt;I wake up about an hour and a half before my family on a normal workday to progress with reading, with writing my blog, and my book projects. A few months ago, I started to feel that things progress very slowly. I spend the same amount of time on writing, but I don't progress well - something that you hopefully haven't realized by reading my articles.&lt;/p&gt;

&lt;p&gt;For some reason, I find it more difficult to deal with context changes than before and it takes me more time to focus on something than before. Ideally, I should understand the reasons behind it so that I can act on the problem. Maybe it's because my topics are more complex than used to be, or at least I demand a deeper analysis from myself, a higher quality - something that you have hopefully seen over the last few years.&lt;/p&gt;

&lt;p&gt;If I want to keep up with the demands of the raising bar that I have set for myself, I have to apply some changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  I changed how I use pomodori
&lt;/h2&gt;

&lt;p&gt;Until I don't understand the root causes of my struggles, I made some immediate changes. Working on different things every day and progressing a little bit is great. It helps you to realize the benefits of &lt;a href="https://www.sandordargo.com/blog/2019/04/17/the-compound-effect"&gt;the compound effect&lt;/a&gt;. But if the taxes of context switching is too high, then it's worth investing the double less frequently, reaping twice the benefits every time (or even more).&lt;/p&gt;

&lt;p&gt;Mathematically it makes sense.&lt;/p&gt;

&lt;p&gt;Imagine that you do something every day and you get 1% better. By the end of the year, you'll be 37 times better. It's maybe inaccurate in this context, but it shows the power of compound interest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1 + 0.01)**365 = 37.78
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you only do the same things every second day, but then you get better by 2% because you invest twice the time on each occasion and you get better focus, you can still get ahead just as far.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1 + 0.02)**183 = 37.48
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is what I'm trying these weeks and months. I have reorganized my work on personal projects in a way that instead of one pomodoro, I spend two consecutive pomodori on a given area a day. I still start my day with half an hour of reading - unless I don't manage to get up, which sometimes happens especially if the kids don't sleep well. Then instead of working half an hour on one and then on a second writing project, I only work on one for a full hour.&lt;/p&gt;

&lt;p&gt;Maybe it's just the power of the new approach, but so far it works better.&lt;/p&gt;

&lt;p&gt;It usually takes some time to get focused, but then I can get into a flow-like state and I can make bigger progress.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some smaller adjustments
&lt;/h2&gt;

&lt;p&gt;You can usually read that the way you do one thing is the way you do everything. If you manage to change one aspect of your life, it will have an effect on all the others. I want to become better focused and I think there are a couple of things that are in my way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Social media
&lt;/h3&gt;

&lt;p&gt;When I'm tired - that can be during the day - or when I'm just waiting for a task to finish, I tend to open social media and not do there anything useful. I just scroll. I'm not even willing to enter into any discussion because it never ends well.&lt;/p&gt;

&lt;p&gt;So instead, I try to keep a list of articles open and just resume those when I have some time to kill, up to a few minutes. I try to get ahead by reading &lt;a href="https://github.com/sandordargo/cpp-resources/blob/master/blogs.md"&gt;C++-related articles&lt;/a&gt; or &lt;a href="https://newsletter.pragmaticengineer.com/"&gt;The Pragmatic Engineer&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calories
&lt;/h3&gt;

&lt;p&gt;When I left behind the office because of Covid, I quite quickly lost about 3 kilograms. I guess all fat. The reason was that in the office, there was always an occasion. Someone brought some pastries, a cake, whatever. They were not replacing my breakfast at home. They were additional calories.&lt;/p&gt;

&lt;p&gt;This disappeared when I started to work from home and the results were visible - at least on the scale.&lt;/p&gt;

&lt;p&gt;With years this started to change. And I have often gone out to the kitchen looking for something to eat, usually in the late afternoon even though I cut some fruits each morning for an afternoon snack. I decided to cut these afternoon journeys to the kitchen. As I started to associate most of these items with sugary industrial garbage, I tend not to desire them. The best would be not to have them at home, but hey, I don't live alone.&lt;/p&gt;

&lt;p&gt;I also like to drink 1-2 beers or glasses of wine a few evenings a week and they are often accompanied by a handful of peanuts. Obviously, I started to limit these items too. An easy thing was to buy less peanuts, so if there is no more, there is no more. You could argue that I should do the same with alcohol, but I buy them when they are on sale. But if there is no beer in the fridge, who would drink it? So that's a solution. And anyway, I find it much easier to say no to these items as they are associated with bad habits and addiction, not like peanuts which contain a load of calories but are healthy as well.&lt;/p&gt;

&lt;p&gt;With these changes and my rigorous evening walks I start to lose the excess. I mean we talk about an excess of less than 10kgs, not a huge amount, but you must take things under control while they are easily manageable.&lt;/p&gt;

&lt;p&gt;In any case, in my opinion, going for a walk every day is a must when you work from home both for physical and mental reasons.&lt;/p&gt;

&lt;h3&gt;
  
  
  Double task list
&lt;/h3&gt;

&lt;p&gt;I started to manage a double task list at work. There is one for the current items that I have to deliver. There might be two projects in parallel or just that they are not broken down as much as I need for my daily work. They are refined enough so that the team understand and decides what to work on, but I prefer to further break it down into bit-sized chunks so that I can more easily pick up the next during the day.&lt;/p&gt;

&lt;p&gt;Also, if I have to wait on a longer local build or on the CI, it's good if I can pick up something. Reading the above-mentioned articles for half an hour is nice and dandy but probably not the most productive.&lt;/p&gt;

&lt;p&gt;Hence I started a second talk list that contain smaller and not very important items. Some cleanup tasks, small experiments, things that are not related closely to my deliverables, and mostly things that nobody asked for. If I have some time to kill, I pick something from there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Goals everywhere
&lt;/h3&gt;

&lt;p&gt;I've been setting daily/weekly/monthly goals for a long time and now I (re)started to do it also at work. I close each day by setting the goals for the next and trying to make sure that at least one of the 3 items I hit already before lunch. It helps me stay focused and also to be more confident.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I wanted to write this article to share a bit about my struggles and the experiments, and solutions I've come up with and I'm more than happy if you also share how you manage to keep up with life. I'm lucky because I don't have to work at a second job and that I can get up in the morning and work on some personal projects and that I can leave the apartment in the evening, the kids are not sleeping alone. I find the idea of only working and taking care of the family and chores without almost no personal time terrifying... I'm lucky. You need something else, something personal to fulfil you.&lt;/p&gt;

&lt;p&gt;We all have only so much time. It might not be up to us to decide on our responsibilities, but it's completely up to us, how we manage the time we have and we better do it well, as time is the only resource that we cannot get back if we waste it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>personalgrowth</category>
      <category>watercooler</category>
      <category>timemanagement</category>
    </item>
    <item>
      <title>C++23: two additional noexcept functions</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 31 May 2023 07:58:56 +0000</pubDate>
      <link>https://dev.to/sandordargo/c23-two-additional-noexcept-functions-53c3</link>
      <guid>https://dev.to/sandordargo/c23-two-additional-noexcept-functions-53c3</guid>
      <description>&lt;p&gt;If my math is correct, there are 125 changes / fixes / new features in C++23 and we are progressively covering them on this blog. I try to go from topic to topic. There are some topics with many smaller changes, such as &lt;a href="https://www.sandordargo.com/blog/2023/05/24/cpp23-constexpr"&gt;&lt;code&gt;constexpr&lt;/code&gt;&lt;/a&gt;, there are some significant topics where even one topic must be in its own post such as &lt;a href="https://www.sandordargo.com/blog/2022/09/21/cpp23-stacktrace-library"&gt;the stacktrace library&lt;/a&gt;, and there are also some shorter posts with few and quite small changes on a given topic. Today we are going to discuss &lt;code&gt;noexcept&lt;/code&gt; related changes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you haven't read it, I'd recommend reading my article on &lt;code&gt;noexcept&lt;/code&gt; and its effects &lt;a href="https://www.sandordargo.com/blog/2023/03/29/binary-size-and-exceptions"&gt;on binary size&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can definitely see two trends in the proposals accepted for the latest standards. They try to make more and more functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.sandordargo.com/blog/2023/05/24/cpp23-constexpr"&gt;&lt;code&gt;constexpr&lt;/code&gt;&lt;/a&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;noexcept&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The two main changes presented today fit into this trend. But let's start with discussing a bit of &lt;code&gt;noexcept&lt;/code&gt; policies in the standard.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;noexcept&lt;/code&gt;, but with conditions
&lt;/h2&gt;

&lt;p&gt;The current policies on whether something in the standard can be &lt;code&gt;noexcept&lt;/code&gt; or not is defined by &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1656r2.html"&gt;P1656R2&lt;/a&gt;. Let me summarize the gist of it here.&lt;/p&gt;

&lt;p&gt;As we got used to it, a destructor should never throw and therefore even if you don't mark it &lt;code&gt;noexcept&lt;/code&gt; they implicitly are!&lt;/p&gt;

&lt;p&gt;There might be standard library functions marked unconditionally &lt;code&gt;noexcept&lt;/code&gt; if the committee fully agrees that the given function cannot throw.&lt;/p&gt;

&lt;p&gt;The standard specifies a couple of special member functions and library functions that can be marked conditionally &lt;code&gt;noexcept&lt;/code&gt;, based on the underlying types and the types these functions operate on. Nothing else can be marked &lt;code&gt;noexcept&lt;/code&gt;, except for library functions that are designed for compatibility with C. Those can be unconditionally &lt;code&gt;noexcept&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Most often, we mark functions either &lt;code&gt;noexcept&lt;/code&gt; or not. So we often mark our functions unconditionally &lt;code&gt;noexcept&lt;/code&gt;. But &lt;code&gt;noexcept&lt;/code&gt; can take a compile-time computable condition, such as &lt;code&gt;std::is_nothrow_move_constructible_v&amp;lt;T&amp;gt; &amp;amp;&amp;amp; std::is_nothrow_assignable_v&amp;lt;T&amp;amp;, U&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the list that can be marked conditionally &lt;code&gt;noexcept&lt;/code&gt; according to C++20.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::swap&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the copy-constructor and -assignment operator&lt;/li&gt;
&lt;li&gt;the move-constructor and -assignment operator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This list is getting modified in the C++23 standard.&lt;/p&gt;

&lt;p&gt;It's also worth noting that an implementation can mark conditionally &lt;code&gt;noexcept&lt;/code&gt; a function even if it's not listed by the standard so.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add a conditional noexcept specification to &lt;code&gt;std::exchange&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One of the primary use cases for &lt;code&gt;std::exchange&lt;/code&gt; is implementing the move constructor and move assignment operator. In a certain way, it's quite similar to &lt;code&gt;std::swap&lt;/code&gt;. Yet, while &lt;code&gt;std::swap&lt;/code&gt; and move operations can be conditionally &lt;code&gt;noexcept&lt;/code&gt;, it was not the case for &lt;code&gt;std::exchange&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Up until C++23.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2401r0.html"&gt;P2401R0&lt;/a&gt; makes &lt;code&gt;std::exchange&lt;/code&gt; conditionally &lt;code&gt;noexcept&lt;/code&gt;. The conditions are the same as for move operations: &lt;code&gt;is_nothrow_move_constructible_v&amp;lt;T&amp;gt; &amp;amp;&amp;amp; is_nothrow_assignable_v&amp;lt;T&amp;amp;, U&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add a conditional noexcept specification to &lt;code&gt;std::apply&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;With &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2321r2.html"&gt;the introduction of &lt;code&gt;zip&lt;/code&gt; algorithms in C++23&lt;/a&gt;, people in and around the committee started to talk once again more and more about &lt;code&gt;std::apply&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason is that &lt;code&gt;std::apply&lt;/code&gt; could be effectively used to implement these new algorithms. In fact, in the previously referenced proposal &lt;code&gt;apply&lt;/code&gt; appears quite a few times. Sadly, &lt;code&gt;std::apply&lt;/code&gt; is not &lt;code&gt;noexcept&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, if we have a look into the exposition-only implementation of &lt;code&gt;apply&lt;/code&gt;, we can see that it uses &lt;code&gt;invoke&lt;/code&gt; and &lt;code&gt;get&lt;/code&gt;. The latter is &lt;code&gt;noexcept&lt;/code&gt; and the former is conditionally &lt;code&gt;noexcept&lt;/code&gt;, so there is no reason why &lt;code&gt;std::apply&lt;/code&gt; should not be conditionally &lt;code&gt;noexcept&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that becomes the new reality with C++23:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tuple&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="nf"&gt;decltype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tuple&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;see&lt;/span&gt; &lt;span class="n"&gt;below&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Let I be the pack 0, 1, ..., (tuple_size_v&amp;lt;remove_reference_t&amp;lt;Tuple&amp;gt;&amp;gt;-1). The exception specification is equivalent to: noexcept(invoke(std::forward&amp;lt;F&amp;gt;(f), get&amp;lt;I&amp;gt;(std::forward&amp;lt;Tuple&amp;gt;(t))...)).&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we reviewed how the standard defines its policies towards the &lt;code&gt;noexcept&lt;/code&gt; specification and we also see that two standard library functions (&lt;code&gt;std::apply&lt;/code&gt; and &lt;code&gt;std::exchange&lt;/code&gt;) are becoming &lt;code&gt;noexcept&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>cpp23</category>
      <category>noexcept</category>
      <category>exceptions</category>
    </item>
    <item>
      <title>C++23: Even more constexpr</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 24 May 2023 08:28:09 +0000</pubDate>
      <link>https://dev.to/sandordargo/c23-even-more-constexpr-3iak</link>
      <guid>https://dev.to/sandordargo/c23-even-more-constexpr-3iak</guid>
      <description>&lt;p&gt;Ever since C++ introduced the &lt;code&gt;constexpr&lt;/code&gt; keyword in C++11, each new standard brought us more and more opportunities to make our code increasingly &lt;code&gt;constexpr&lt;/code&gt;, in other words, compile-time execution friendly.&lt;/p&gt;

&lt;p&gt;In this article, we are going to review briefly what changes with C++23 on this front.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-literal variables in &lt;code&gt;constexpr&lt;/code&gt; functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2242r3.html"&gt;P2242R3&lt;/a&gt; proposes to remove the restriction that a &lt;code&gt;constexpr&lt;/code&gt; function cannot contain &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a definition of a variable of a &lt;a href="https://en.cppreference.com/w/cpp/named_req/LiteralType"&gt;non-literal type&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;a definition of a variable of static or thread storage duration,&lt;/li&gt;
&lt;li&gt;or a goto statement,&lt;/li&gt;
&lt;li&gt;or an identifier label.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rationale behind this change is that the presence of the listed things in a function is not a problem as long as they are not evaluated at compile-time. We should remind ourselves that a &lt;code&gt;constexpr&lt;/code&gt; function may or may not be evaluated at compile-time.&lt;/p&gt;

&lt;p&gt;Let's suppose that in a &lt;code&gt;constexpr&lt;/code&gt; function we want to call a piece of code that is guaranteed to be evaluated at compile-time. Then we need to have that piece of code in a block under the condition of either &lt;a href="https://www.sandordargo.com/blog/2022/06/01/cpp23-if-consteval"&gt;&lt;code&gt;if consteval&lt;/code&gt;&lt;/a&gt; or &lt;code&gt;if (std::is_constant_evaluated())&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;With this paper, the following code becomes valid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_constant_evaluated&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This could have been problematic before&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;nonliteral&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nonliteral&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;static_assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nonliteral&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As &lt;code&gt;nonliteral&lt;/code&gt; is a non-literal type, the compilation should fail without this proposal, even though the line that causes the failure is not in a constant-evaluated context.&lt;/p&gt;

&lt;p&gt;This change is available starting from GCC 12 and Clang 15.&lt;/p&gt;

&lt;h2&gt;
  
  
  Relaxing some more &lt;code&gt;constexpr&lt;/code&gt; restrictions
&lt;/h2&gt;

&lt;p&gt;As I mentioned in the intro, every new standard relaxes a bit &lt;code&gt;constexpr&lt;/code&gt; conditions and in fact, even the previous section discussed relaxations. But there is more. Thanks to &lt;a href="https://brevzin.github.io/"&gt;Barry Revzin&lt;/a&gt; and &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2448r2.html"&gt;P2448R2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's see the two main issues the paper wants to solve.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions that are not &lt;em&gt;yet&lt;/em&gt; &lt;code&gt;constexpr&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;As I wrote earlier, a &lt;code&gt;constexpr&lt;/code&gt; function might be evaluated at compile-time but might not. Whether or not a call can be constant-evaluated might depend on the parameters passed in. Often it seems straightforward that a function cannot be constant-evaluated because of the non-&lt;code&gt;constexpr&lt;/code&gt; functions it calls. But never say never! More and more functions in the standard library become &lt;code&gt;constexpr&lt;/code&gt; and conditionally marking functions &lt;code&gt;constexpr&lt;/code&gt; - as in the next happens - is painful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;optional&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#if __cpp_lib_optional &amp;gt;= 202106
&lt;/span&gt;&lt;span class="k"&gt;constexpr&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;h&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explicitly defaulted functions follow different rules
&lt;/h3&gt;

&lt;p&gt;Another problem Barry wanted to address is related to explicitly defaulted functions. Let's use the following template to demonstrate the issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you mark a member function &lt;code&gt;constexpr&lt;/code&gt;, it means that it &lt;em&gt;might&lt;/em&gt; be evaluated in a constant-evaluation context.&lt;/p&gt;

&lt;p&gt;On the other hand, if you &lt;code&gt;default&lt;/code&gt; a member function, it means that it must be &lt;code&gt;constexpr&lt;/code&gt;-compatible in all instantiations. Yet, not all the compilers complain if you violate this rule and those that raise an error, they don't raise the error in each case.&lt;/p&gt;

&lt;p&gt;In the case of a template, this means that all instantiations should be &lt;code&gt;constexpr&lt;/code&gt;-compatible. If that's not possible, the current solution is to remove the &lt;code&gt;constexpr&lt;/code&gt; from the explicitly defaulted constructors and &lt;code&gt;operator==()&lt;/code&gt;. We might end up with an instantiation where they would be usable in a constant-evaluation context, yet they are not marked as &lt;code&gt;constexpr&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is changing after all?
&lt;/h3&gt;

&lt;p&gt;With the acceptance of &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2448r2.html"&gt;this proposal&lt;/a&gt;, there are a couple of restrictions removed.&lt;/p&gt;

&lt;p&gt;Constructors and destructors will follow the same rules as functions in terms of &lt;code&gt;constexpr&lt;/code&gt;. In addition, an explicitly defaulted function on its first declaration will not only be implicitly inline but also implicitly &lt;code&gt;constexpr&lt;/code&gt; if it satisfies the requirements of a &lt;code&gt;constexpr&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;And even &lt;code&gt;constexpr&lt;/code&gt; functions will get some relaxed rules - beyond the relaxation presented in the previous section. They can return and/or take as parameters &lt;a href="https://en.cppreference.com/w/cpp/named_req/LiteralType"&gt;non-literal types&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This change is available starting from GCC 13.&lt;/p&gt;

&lt;h2&gt;
  
  
  Permitting static &lt;code&gt;constexpr&lt;/code&gt; variables in &lt;code&gt;constexpr&lt;/code&gt; functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://wg21.link/P2647R1"&gt;P2647R1&lt;/a&gt; corrects a hole in the standard. As the proposal says, there is no good reason why a &lt;code&gt;constexpr&lt;/code&gt; function cannot have today a &lt;code&gt;static constexpr&lt;/code&gt; local variable.&lt;/p&gt;

&lt;p&gt;While this is fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="nf"&gt;xdigit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0123456789abcdef"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its &lt;code&gt;constexpr&lt;/code&gt; version wouldn't compile without some workarounds presented in the [paper](&lt;a href="https://wg21.link/P2647R1:"&gt;https://wg21.link/P2647R1:&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="nf"&gt;xdigit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0123456789abcdef"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not anymore! The above code is becoming legit. GCC 13 and Clang 16 already support it!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;constexpr type_info::operator==()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;At the moment, &lt;code&gt;typeid&lt;/code&gt; is allowed in constant expressions, but the returned &lt;a href="https://www.sandordargo.com/blog/2023/03/01/binary-sizes-and-rtti#typeid-and-stdtype_info"&gt;&lt;code&gt;std::type_info&lt;/code&gt;&lt;/a&gt; object has no &lt;code&gt;constexpr&lt;/code&gt; methods, therefore it's not practically usable in a &lt;code&gt;constexpr&lt;/code&gt; context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://wg21.link/P1328R1"&gt;P1328R1&lt;/a&gt; makes the equality operator (&lt;code&gt;operator==()&lt;/code&gt;) &lt;code&gt;constexpr&lt;/code&gt; to &lt;code&gt;std::type_info&lt;/code&gt; becomes practically usable in compile-time functions!&lt;/p&gt;

&lt;p&gt;It's fascinating how far the &lt;code&gt;constexpr&lt;/code&gt; came. I mean, &lt;code&gt;typeid()&lt;/code&gt; and &lt;code&gt;std::type_info&lt;/code&gt; are used for &lt;a href="https://www.sandordargo.com/blog/2023/03/01/binary-sizes-and-rtti"&gt;Run-Time Type Information (RTTI)&lt;/a&gt; and yet we talk about &lt;code&gt;constexpr&lt;/code&gt;...&lt;/p&gt;

&lt;p&gt;GCC 12, Clang 17 and MSVC 19.33 already provide this feature!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;constexpr&lt;/code&gt; for &lt;code&gt;&amp;lt;cmath&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;cstdlib&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Until now, &lt;code&gt;&amp;lt;cmath&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;cstdlib&amp;gt;&lt;/code&gt; has barely contained any &lt;code&gt;constexpr&lt;/code&gt; functions. &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf"&gt;P0533R9&lt;/a&gt; aims to improve on this situation in order to facilitate compile-time programming. These headers have been simply neglected so far, otherwise, there is no reason why &lt;code&gt;std::chrono::abs&lt;/code&gt; is &lt;code&gt;constexpr&lt;/code&gt; but &lt;code&gt;std::abs&lt;/code&gt; is not.&lt;/p&gt;

&lt;p&gt;In (8.E-G sections of &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf"&gt;P0533R9&lt;/a&gt; you can find the full list of functions that are going to become &lt;code&gt;constexpr&lt;/code&gt;. Luckily it's quite a long list!&lt;/p&gt;

&lt;p&gt;This is yet to be implemented by the compilers!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;constexpr std::unique_ptr&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2273r3.pdf"&gt;P2273R3&lt;/a&gt; is bringing us &lt;code&gt;constexpr&lt;/code&gt; unique pointers. The requirements of &lt;code&gt;constexpr&lt;/code&gt; were loosened by &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html"&gt;P0784R7&lt;/a&gt;, adopted by C++20. With that &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt; might be used in certain &lt;code&gt;constexpr&lt;/code&gt;-contexts, so it was worth trying making &lt;code&gt;std::unique_ptr&lt;/code&gt; also &lt;code&gt;constexpr&lt;/code&gt;. It worked out fine and it's already available in the new versions of major compilers.&lt;/p&gt;

&lt;p&gt;The authors also tried to make &lt;code&gt;shared_ptr&lt;/code&gt; &lt;code&gt;constexpr&lt;/code&gt;, but due to missing compile-time atomic support it's not possible for the moment to implement &lt;code&gt;shared_ptr&lt;/code&gt; according to the standard. The authors are going to explore their possibilities further.&lt;/p&gt;

&lt;p&gt;GCC 12, Clang 16 and MSVC 19.33 already provide this feature!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;constexpr&lt;/code&gt; for integral overloads of &lt;code&gt;std::to_chars()&lt;/code&gt; and &lt;code&gt;std::from_chars()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;At compile-time, there is currently no standard way to make conversions between numbers and strings. Now that a &lt;code&gt;std::string&lt;/code&gt; can be instantiated at compile-time, we are much closer to get a &lt;code&gt;constexpr std::format&lt;/code&gt; and this is a gap.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::to_chars&lt;/code&gt; and &lt;code&gt;std::from_chars&lt;/code&gt; are fundamental blocks for parsing and formatting as they are locale-independent, they don't throw and don't allocate memory. Except for the floating point overloads now they will become &lt;code&gt;constexpr&lt;/code&gt; thanks to &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2291r3.pdf"&gt;P2291R3&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;They will be another step forward to have &lt;code&gt;constexpr std::format&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;GCC 13, Clang 16 and MSVC 19.34 already provide this feature!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;constexpr std::bitset&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2417r2.pdf"&gt;P2417R2&lt;/a&gt; extends the &lt;code&gt;constexpr&lt;/code&gt; interface of &lt;code&gt;std::bitset&lt;/code&gt;. So far, only one of the constructors and &lt;code&gt;operator[]&lt;/code&gt; was marked as &lt;code&gt;constexpr&lt;/code&gt;. However, &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0980r1.pdf"&gt;since &lt;code&gt;std::string&lt;/code&gt; can be &lt;code&gt;constexpr&lt;/code&gt;&lt;/a&gt;, all the internals - and therefore the full API - of &lt;code&gt;std::bitset&lt;/code&gt; can be &lt;code&gt;constexpr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;GCC 13, Clang 16 and MSVC 19.34 already provide this feature!&lt;/p&gt;

&lt;h2&gt;
  
  
  DR: constexpr for std::optional and std::variant
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2231r1.html"&gt;P2231R1&lt;/a&gt; adds some missing &lt;code&gt;constexpr&lt;/code&gt; both to &lt;code&gt;std::optional&lt;/code&gt; and &lt;code&gt;std::variant&lt;/code&gt;. As the already referenced &lt;a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html"&gt;P0784R7&lt;/a&gt; made it possible to use &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt; at compile-time (by using &lt;code&gt;std::construct_at&lt;/code&gt;), there was no reason to not making &lt;code&gt;optional&lt;/code&gt; and &lt;code&gt;variant&lt;/code&gt; fully constexpr.&lt;/p&gt;

&lt;p&gt;GCC 11, Clang 13 and MSVC 19.31 already provide this fix!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we reviewed the &lt;code&gt;constexpr&lt;/code&gt; related changes in C++23. There are quite many of them, and our options for compile-time programming are growing! You can check on &lt;a href="https://en.cppreference.com/w/cpp/23"&gt;C++ Reference&lt;/a&gt; whether they are implemented by your compiler of choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>cpp23</category>
      <category>constexpr</category>
      <category>compiletime</category>
    </item>
    <item>
      <title>Are we - developers - spoiled or abused?</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 17 May 2023 08:04:36 +0000</pubDate>
      <link>https://dev.to/sandordargo/are-we-developers-spoiled-or-abused-14g</link>
      <guid>https://dev.to/sandordargo/are-we-developers-spoiled-or-abused-14g</guid>
      <description>&lt;p&gt;I think we are lucky as software developers. Most of us like what we do and we get a good salary. Even if a lot of people do not like the company they work for, in general, they like software development and they have the possibility to learn a little bit more and look for another job.&lt;/p&gt;

&lt;p&gt;Even if we tend to complain about the salaries we make, and it might be that we are underpaid compared to some others, in general, we earn a good salary and only a few professions let people earn and live as well.&lt;/p&gt;

&lt;p&gt;I know that sitting all day has its severe dangers on our health, but our decent salaries come with an essentially safe job.&lt;/p&gt;

&lt;p&gt;Outsiders would tend to tell that we are spoiled.&lt;/p&gt;

&lt;p&gt;But the feeling of developers is often that we are severely abused.&lt;/p&gt;

&lt;p&gt;I guess both groups are right in certain aspects.&lt;/p&gt;

&lt;p&gt;Let me share a story with you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mo' responsibilities but no mo' bucks
&lt;/h2&gt;

&lt;p&gt;I've met recently a friend of mine, who told me that he is finally on the lookout for a new job. He had told me some time ago, that'd stay a bit more time in his current role, so I became quickly interested in why he changed his mind.&lt;/p&gt;

&lt;p&gt;He told me that got promoted! That's good news I thought.&lt;/p&gt;

&lt;p&gt;So he was told that now he had the role to deploy in production, he is expected to review code and he'd have to investigate bugs in production.&lt;/p&gt;

&lt;p&gt;As I didn't want to stop his flow of words, I kept on listening amazed. I personally think that every engineer should do &lt;a href="https://www.sandordargo.com/blog/2021/10/06/airy-code-reviews"&gt;code reviews&lt;/a&gt;. Maybe, just maybe, not everyone's approval should count the same way, but juniors should also perform code reviews. At least for three different reasons.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They often spot such typos that others wouldn't, because they focus on what they can with their experience. This was meant to be a compliment, they are often very thorough. At the same time, they often lack the bigger picture, just because of the shorter time spent around.&lt;/li&gt;
&lt;li&gt;As they have fewer preconceptions and assumptions, they often ask very different questions in code reviews than more experienced colleagues. Those questions often help both reveal problems the authors wouldn't have thought of and put problems into a different context.&lt;/li&gt;
&lt;li&gt;Code reviews are also a great tool to learn from more experienced authors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I was also surprised by the fact that juniors don't have to do production investigations. Again, they are such a great tool for learning even if you have to sometimes ask for help from more experienced colleagues.&lt;/p&gt;

&lt;p&gt;But each company, each team forms its own culture, I'm not here to judge...&lt;/p&gt;

&lt;p&gt;It's clear that at this place juniors are deprived of tasks and responsibilities, while medior and senior engineers have both more tasks and responsibilities.&lt;/p&gt;

&lt;p&gt;When this friend of mine asked his management what would be his new salary he was told that there is no increase, no new salary. After all, he's still a programmer who'd just write code.&lt;/p&gt;

&lt;p&gt;What the hell?&lt;/p&gt;

&lt;p&gt;At my previous job when I was promoted to a senior role, my senior manager even took pride in handing me over the promotion mail with the new salary. I wouldn't say it was a huge increase, it was double of a normal yearly increase. Yet, it was still at least something and the increased level of responsibility and expectations were acknowledged.&lt;/p&gt;

&lt;p&gt;Is this friend of mine spoiled or abused? Are we developers in general spoiled or abused?&lt;/p&gt;

&lt;h2&gt;
  
  
  No matter the money, fairness is important
&lt;/h2&gt;

&lt;p&gt;I personally think that no matter how much we earn, we should be treated fairly and an increased level of responsibility should be paid with a higher salary. At the same time, I think it's also reasonable to expect you to already perform on that level before you get the position.&lt;/p&gt;

&lt;p&gt;In my previous jobs - as a DBA in one and a software engineer in the other - people could only be promoted if they performed their new tasks for a couple of months and management was happy about their performance. When I heard about this for the first time, I was surprised and a bit angry. What? I should perform more for the same money? But I realized that this makes sense.&lt;/p&gt;

&lt;p&gt;What if you are a good coder, but you cannot be a good mentor? What if you cannot communicate and you cannot review code without killing the morale of the team? What if you cannot handle the pressure that comes with investigating production incidents? Then you clearly don't merit the position where these are expected, and &lt;a href="https://www.sandordargo.com/blog/2022/02/09/5-types-of-communication-a-senior-developer-does"&gt;these tasks, including communication, are expected from senior engineers&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;It doesn't mean that you're bad at what you had been doing, it means that you're not ready for the new position. Or maybe you don't even want to be in that position! Not everyone has to be a mentor and accept a certain level of technical leadership.&lt;/p&gt;

&lt;p&gt;Demoting someone can be a big loss of face. I think it's better if our current abilities and readiness are slowly assessed based on real-life performance before a promotion. I think it's even better if we pick up these responsibilities gradually. Then once we have enough of them and we perform in those well enough, then we can get our promotion - along with the salary increase.&lt;/p&gt;

&lt;p&gt;Now you might claim that if those responsibilities come gradually, then the salary increase should be gradual too and the promotion shouldn't be a big hike. You should get a few smaller increases over the years - beyond the normal yearly increase. That's also a way, still, I think that a promotion should always come with a certain level of salary increase.&lt;/p&gt;

&lt;p&gt;If that's not possible due to some economic circumstances, make sure as an employer that you emphasize those circumstances and promise that as soon as the wartime ends, you compensate for the missed increase. And of course, don't forget about it...&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Are we spoiled or abused? In certain ways, both can be true. Our field is very competitive and we are asked a lot in order to get a good job. Sometimes even in order to get a bad job. On the other hand, we are paid a relatively high salary and we can complain about things that make us seem spoiled.&lt;/p&gt;

&lt;p&gt;On the other hand, there are many bad practices in the industry, in our management that might make you feel abused if you experience it. One of these things is getting a promotion with increased responsibilities and more tasks but without a salary increase.&lt;/p&gt;

&lt;p&gt;I think that no matter how much we earn we should be treated fairly and part of this fairness is that you don't handle a promotion in two different ways. Saying that now you have to do this and that in addition, but then claiming that your job is still the same, so you don't qualify for an increase is not what I call fair treatment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>watercooler</category>
      <category>career</category>
      <category>jobhunt</category>
      <category>satisfaction</category>
    </item>
    <item>
      <title>Is this dynamic_cast needed?</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 10 May 2023 08:02:48 +0000</pubDate>
      <link>https://dev.to/sandordargo/is-this-dynamiccast-needed-440l</link>
      <guid>https://dev.to/sandordargo/is-this-dynamiccast-needed-440l</guid>
      <description>&lt;p&gt;I have already written a couple of times about &lt;code&gt;dynamic_cast&lt;/code&gt;. I claimed that &lt;a href="https://www.sandordargo.com/blog/2023/03/01/binary-sizes-and-rtti"&gt;if you can avoid using it and RTTI, you can get a smaller binary&lt;/a&gt;. I also claimed that &lt;a href="https://www.sandordargo.com/blog/2023/04/26/without-rtti-your-code-will-be-cleaner"&gt;without &lt;code&gt;dynamic_cast&lt;/code&gt; your code will be cleaner&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first claim is new from my side, I didn't care about executable size earlier. The second one is less so, I read a long time ago &lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c146-use-dynamic_cast-where-class-hierarchy-navigation-is-unavoidable"&gt;in the Core Guidelines&lt;/a&gt; that one should avoid using &lt;code&gt;dynamic_cast&lt;/code&gt; whenever possible, but there are some cases when you cannot avoid it.&lt;/p&gt;

&lt;p&gt;I've discussed this topic with a friend of mine who's been teaching C++ for a couple of years and I know he doesn't share these views that much. Or at least he's not so critical towards &lt;code&gt;dynamic_cast&lt;/code&gt;. He thinks that it is a useful tool in many cases and there must be a reason why it's in the language and not removed.&lt;/p&gt;

&lt;p&gt;It is true that is mentioned in the Core Guidelines that there can be some cases when it is needed. Besides, one of &lt;a href="https://www.youtube.com/watch?v=5P7fnH9cCR0"&gt;the greatest superpowers of C++ is backward compatibility&lt;/a&gt;. Removing &lt;code&gt;dynamic_cast&lt;/code&gt; would break half of the world...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;dynamic_cast&lt;/code&gt; safely converts pointers and references to classes up, down and sideways along the inheritance hierarchy&lt;/em&gt; - according to &lt;a href="https://en.cppreference.com/w/cpp/language/dynamic_cast"&gt;CppReference&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This friend of mine sends me some piece of code every now and then saying that this might be a good example. He sent me an example a few weeks ago and maybe this was the good one. Maybe.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tree combining templates and virtuals
&lt;/h2&gt;

&lt;p&gt;The example is from &lt;a href="https://www.amazon.com/Category-Theory-Programmers-Bartosz-Milewski/dp/0464243874/ref=sr_1_1?crid=SBQNZW8VS5A&amp;amp;keywords=Category+Theory+for+Programmers&amp;amp;qid=1683655205&amp;amp;s=books&amp;amp;sprefix=category+theory+for+programmers+%252Cstripbooks-intl-ship%252C146&amp;amp;sr=1-1&amp;amp;_encoding=UTF8&amp;amp;tag=sandordargo-20&amp;amp;linkCode=ur2&amp;amp;linkId=dd03bab283cd69a4cb78912fcad27448&amp;amp;camp=1789&amp;amp;creative=9325"&gt;Category Theory for Programmers&lt;/a&gt; written by &lt;a href="https://bartoszmilewski.com/"&gt;Bartosz Milewski&lt;/a&gt; who also wrote &lt;a href="https://www.amazon.com/Action-Industrial-Strength-Programming-Techniques/dp/0201699486/ref=sr_1_1?crid=1BJUHHH2PO1L0&amp;amp;keywords=C%252B%252B+In+Action%253A+Industrial+Strength+Programming+Techniques&amp;amp;qid=1683655290&amp;amp;s=books&amp;amp;sprefix=c%252B%252B+in+action+industrial+strength+programming+techniques%252Cstripbooks-intl-ship%252C154&amp;amp;sr=1-1&amp;amp;_encoding=UTF8&amp;amp;tag=sandordargo-20&amp;amp;linkCode=ur2&amp;amp;linkId=4818b2ac47747ebe901c75b163d8d3a6&amp;amp;camp=1789&amp;amp;creative=9325"&gt;C++ In Action: Industrial Strength Programming Techniques&lt;/a&gt;. He shows that we should be able to write/recognize some algebraic data structures in C++ and implement &lt;code&gt;fmap&lt;/code&gt; for them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;fmap&lt;/code&gt; is a higher-order function in functional programming that applies a given function to the elements of a container and returns a new container with the results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here is his implementation for a &lt;code&gt;Tree&lt;/code&gt; and &lt;code&gt;fmap()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Tree&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Leaf&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;_label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pl&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;_label&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                          &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is that a good implementation? What is good anyway? The author explicitly writes that he omitted resource and memory management and in production code, one should use smart pointers. I think it's a meaningful simplification in a book or in a blog post.&lt;/p&gt;

&lt;p&gt;Let's concentrate on &lt;code&gt;fmap()&lt;/code&gt; first and just take note that it's probably an acceptable idea to return &lt;code&gt;nullptr&lt;/code&gt; if both casts fail. I'd probably throw an exception instead if exceptions are allowed, otherwise, let's say that this is fine.&lt;/p&gt;

&lt;p&gt;But what about the &lt;code&gt;dynamic_cast&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;My first idea was that hey, we should replace the one "big" &lt;code&gt;fmap()&lt;/code&gt; with 2 overloads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="nf"&gt;fmap2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Leaf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;fmap2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmap2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                        &lt;span class="n"&gt;fmap2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in this case, the compilation fails!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;: In instantiation of 'Node&amp;lt;B&amp;gt;* fmap2(std::function&amp;lt;B(A)&amp;gt;, Node&amp;lt;A&amp;gt;*) [with A = int; B = float]':
&amp;lt;source&amp;gt;:82:28:   required from here
&amp;lt;source&amp;gt;:68:32: error: no matching function for call to 'fmap2&amp;lt;int&amp;gt;(std::function&amp;lt;float(int)&amp;gt;&amp;amp;, Tree&amp;lt;int&amp;gt;*&amp;amp;)'
   68 |     return new Node&amp;lt;B&amp;gt;(fmap2&amp;lt;A&amp;gt;(f, t-&amp;gt;_left),
      |                        ~~~~~~~~^~~~~~~~~~~~~
&amp;lt;source&amp;gt;:62:10: note: candidate: 'template&amp;lt;class A, class B&amp;gt; Leaf&amp;lt;B&amp;gt;* fmap2(std::function&amp;lt;B(A)&amp;gt;, Leaf&amp;lt;A&amp;gt;*)'
   62 | Leaf&amp;lt;B&amp;gt;* fmap2(std::function&amp;lt;B(A)&amp;gt; f, Leaf&amp;lt;A&amp;gt;* t) {
      |          ^~~~~
&amp;lt;source&amp;gt;:62:10: note:   template argument deduction/substitution failed:
&amp;lt;source&amp;gt;:68:39: note:   cannot convert 't-&amp;gt;Node&amp;lt;int&amp;gt;::_left' (type 'Tree&amp;lt;int&amp;gt;*') to type 'Leaf&amp;lt;int&amp;gt;*'
   68 |     return new Node&amp;lt;B&amp;gt;(fmap2&amp;lt;A&amp;gt;(f, t-&amp;gt;_left),
      |                                    ~~~^~~~~
&amp;lt;source&amp;gt;:67:10: note: candidate: 'template&amp;lt;class A, class B&amp;gt; Node&amp;lt;B&amp;gt;* fmap2(std::function&amp;lt;B(A)&amp;gt;, Node&amp;lt;A&amp;gt;*)'
   67 | Node&amp;lt;B&amp;gt;* fmap2(std::function&amp;lt;B(A)&amp;gt; f, Node&amp;lt;A&amp;gt;* t) {
      |          ^~~~~
&amp;lt;source&amp;gt;:67:10: note:   template argument deduction/substitution failed:
&amp;lt;source&amp;gt;:68:39: note:   cannot convert 't-&amp;gt;Node&amp;lt;int&amp;gt;::_left' (type 'Tree&amp;lt;int&amp;gt;*') to type 'Node&amp;lt;int&amp;gt;*'
   68 |     return new Node&amp;lt;B&amp;gt;(fmap2&amp;lt;A&amp;gt;(f, t-&amp;gt;_left),
      |                                    ~~~^~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course! What if &lt;code&gt;t&lt;/code&gt; is a &lt;code&gt;Tree&amp;lt;A&amp;gt;&lt;/code&gt; and not a &lt;code&gt;Leaf&lt;/code&gt; of a &lt;code&gt;Node&lt;/code&gt;?! That shouldn't happen, but even in the original code we have a case to handle that, so let's just add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="nf"&gt;fmap2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the code compiles, but the executable returned &lt;em&gt;139&lt;/em&gt;. In other words, we have a segmentation fault.&lt;/p&gt;

&lt;p&gt;Right, &lt;code&gt;Node&lt;/code&gt; has two pointers to &lt;code&gt;Tree&lt;/code&gt; and instead of matching one of the overloads for &lt;code&gt;Leaf&lt;/code&gt; or &lt;code&gt;Node&lt;/code&gt;, the one for &lt;code&gt;Tree&lt;/code&gt; is matched which returns a &lt;code&gt;nullptr&lt;/code&gt; and when we try to print the labels we crash.&lt;/p&gt;

&lt;p&gt;In case of function overloads the static type is matched. If we want runtime dispatching, we need &lt;code&gt;virtual&lt;/code&gt; functions and overrides.&lt;/p&gt;

&lt;p&gt;So what if we'd implement a &lt;em&gt;virtual&lt;/em&gt; &lt;code&gt;clone&lt;/code&gt; function? The initial idea might seem nice, but the problem is that we'd also have to either apply &lt;code&gt;f&lt;/code&gt; on the member or just use its return type (marked by &lt;code&gt;typename B&lt;/code&gt;). In order to do so, we could apply an extension of the prototype design pattern, where a &lt;em&gt;virtual&lt;/em&gt; &lt;code&gt;clone()&lt;/code&gt; method replaces a &lt;em&gt;virtual&lt;/em&gt; constructor which doesn't exist in C++. The only problem is that we would need to extend the pattern by passing the transformation function (&lt;code&gt;f&lt;/code&gt;) to the &lt;code&gt;clone()&lt;/code&gt; as a parameter. But &lt;code&gt;f&lt;/code&gt; is a template type and we cannot combine a virtual function with a template.&lt;/p&gt;

&lt;p&gt;We'd need to somehow erase &lt;code&gt;f&lt;/code&gt;'s return type by the time we reach &lt;code&gt;clone()&lt;/code&gt;. But you have to know where to stop and when going down the rabbit hole is not worth it - in my opinion.&lt;/p&gt;

&lt;p&gt;Let's just accept that &lt;code&gt;dynamic_cast&lt;/code&gt; has its merit with &lt;em&gt;this&lt;/em&gt; &lt;code&gt;Tree&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;But do we really need it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Prefer composition over inheritance
&lt;/h2&gt;

&lt;p&gt;Probably we have all heard in many places that we should prefer composition over inheritance.&lt;/p&gt;

&lt;p&gt;We are also probably familiar with the KISS principle which means that we should &lt;em&gt;keep things simple, stupid&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now let's have a look at the above &lt;code&gt;Node&lt;/code&gt; class once again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;_right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a derived class inheriting from a templated base class. This derived class stores two pointers to objects of the base class type. We use templates, composition and inheritance at the same time. That cannot be KISS.&lt;/p&gt;

&lt;p&gt;I don't think that it's needed.&lt;/p&gt;

&lt;p&gt;We do need templates given that we want the ability to store different types in the tree.&lt;/p&gt;

&lt;p&gt;We also need composition, as in a node we want to store references to the underlying two trees.&lt;/p&gt;

&lt;p&gt;To simplify the &lt;code&gt;Tree&lt;/code&gt; class and also hope to remove the need for &lt;code&gt;dynamic_cast&lt;/code&gt;, let's get rid of the class hierarchy.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(I'm also omitting the issue of memory management as I want the two solutions to remain easily comparable)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What does that inheritance give us anyway?&lt;/p&gt;

&lt;p&gt;Just by looking at the type, we know whether we deal with an intermediary node or a leaf in the tree. But we can know that in other ways too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;V&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tree&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

 &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;nullopt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above &lt;code&gt;Tree&lt;/code&gt; class we have three members. Two pointers two other &lt;code&gt;Tree&lt;/code&gt;s and we also store an optional label.&lt;/p&gt;

&lt;p&gt;We expose two constructors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the one taking two pointers to &lt;code&gt;Tree&lt;/code&gt;s initializes an object that corresponds to the former &lt;code&gt;Node&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;the other taking an instance of the template argument type initializes an object that will serve as a &lt;code&gt;Leaf&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the members are private, we either use &lt;code&gt;Tree&lt;/code&gt; one way or the other. By looking at the initialization, we'll know which way it is used. If you really want to know it during runtime, we could query an additional member &lt;code&gt;bool is_leaf;&lt;/code&gt; which would be initialized in the constructor call. But I don't think that we need that knowledge. &lt;em&gt;(If the &lt;code&gt;label&lt;/code&gt; cannot be &lt;code&gt;nullopt&lt;/code&gt; in a leaf, we could also check the state of the optional &lt;code&gt;label&lt;/code&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Also, we'd need to expose certain getters to the members of this class, but we don't need them for the &lt;code&gt;fmap()&lt;/code&gt; implementation. You might argue that if the accessors are non-&lt;code&gt;const&lt;/code&gt; then people can misuse the class. And that's a valid concern. But it takes extra effort (&lt;em&gt;why would you do that?&lt;/em&gt;) and it strikes out in a pull request, it's easy to spot such misuses (&lt;em&gt;why would you approve that?&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fmap()&lt;/code&gt; implementation is fairly simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;V&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;N&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="nf"&gt;fmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                    &lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;nullopt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;t&lt;/code&gt; is &lt;code&gt;nullptr&lt;/code&gt; then we stop the recursion by returning &lt;code&gt;nullptr&lt;/code&gt;. Otherwise, we return a new &lt;code&gt;Tree&lt;/code&gt; using the mapped type (&lt;code&gt;V -&amp;gt; N&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;In order to make this work, we need a new &lt;code&gt;Tree&lt;/code&gt; constructor that can initialize all the members. But nobody else needs that so let's make it &lt;code&gt;private&lt;/code&gt; and make &lt;code&gt;fmap()&lt;/code&gt; a &lt;code&gt;friend&lt;/code&gt; of &lt;code&gt;Tree&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;V&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tree&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

 &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;V2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;N&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;friend&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;fmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;V2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Tree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;*&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;nullopt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Which one is better?
&lt;/h2&gt;

&lt;p&gt;I don't have a clear answer.&lt;/p&gt;

&lt;p&gt;In the original solution, you can see from the (dynamic) type whether you deal with a node or a leaf. But you also need a polymorphic structure and all the overhead that comes with it. You don't only see this characteristic, but each type represents a clear role. &lt;/p&gt;

&lt;p&gt;You might say that my solution is not complete because we need to expose the members, but there is a very high chance that the original &lt;code&gt;Tree&lt;/code&gt; wouldn't stay a &lt;code&gt;struct&lt;/code&gt; and the members would be &lt;code&gt;private&lt;/code&gt; in that too. But even in that case, the second solution (if the accessors are non-&lt;code&gt;const&lt;/code&gt;) might be misused. Even though it's not convenient and it is highly visible.&lt;/p&gt;

&lt;p&gt;But what about performance?&lt;/p&gt;

&lt;p&gt;We can expect that the second solution will have a bigger memory footprint as in every case each node of the &lt;code&gt;Tree&lt;/code&gt; has 3 members. Two pointers and an optional &lt;code&gt;V&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, we can expect that the second solution will be faster, as there are no &lt;code&gt;virtual&lt;/code&gt; functions, there is no dynamic dispatching of function calls and obviously no casting.&lt;/p&gt;

&lt;p&gt;I created a small &lt;code&gt;Tree&lt;/code&gt; of 8 integers and applied a function on it which multiplies each value by 2 and ran it 10,000 times. The static solution took a bit longer time to compile (~3%), and its executable was a tiny bit smaller (~2%), but it was executing much faster (~50%). Even though as expected, the memory consumption was 30% higher for the non-polymorphic version.&lt;/p&gt;

&lt;p&gt;So the second solution is smaller, much faster, but it needs way more memory at runtime. It's simpler, but in a way, it's less expressive.&lt;/p&gt;

&lt;p&gt;In most cases, there is no black or white. Only tradeoffs. In this case, we traded off memory for runtime. Simplicity for expressiveness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Today, we talked once again about when (not) to use &lt;code&gt;dynamic_cast&lt;/code&gt;. We looked into a tree implementation where &lt;code&gt;fmap()&lt;/code&gt; needed to use &lt;code&gt;dynamic_cast&lt;/code&gt;. Then we looked into another where it was not needed. The &lt;code&gt;dynamic_cast&lt;/code&gt; version needs more time to execute but much less memory than the non-polymorphic version.&lt;/p&gt;

&lt;p&gt;Programming, just like life, is about tradeoffs. In this case, the solution with &lt;code&gt;dynamic_cast&lt;/code&gt; has its merits, but you might go with another solution depending on your constraints.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>dyamiccast</category>
      <category>virtual</category>
    </item>
    <item>
      <title>Variadic functions vs variadic templates</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 03 May 2023 09:17:13 +0000</pubDate>
      <link>https://dev.to/sandordargo/variadic-functions-vs-variadic-templates-3c1a</link>
      <guid>https://dev.to/sandordargo/variadic-functions-vs-variadic-templates-3c1a</guid>
      <description>&lt;p&gt;A few months ago, I wrote a review on &lt;a href="https://www.sandordargo.com/blog/2022/10/28/template-metaprogramming-with-cpp-by-marius-bancila"&gt;Template Metaprogramming with C++ by Marius Bancila&lt;br&gt;
&lt;/a&gt; where I mentioned not only that it's a great book, but also that there are some topics which I'll cover more in detail. Some time ago we discussed constructor templates and today I want to discuss variadic functions and variadic function templates.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are variadic functions?
&lt;/h2&gt;

&lt;p&gt;Even if you don't explicitly know about variadic functions, you have most probably used the &lt;code&gt;printf()&lt;/code&gt; family in C and/or C++. A variadic function is a function that can take an arbitrary number of arguments of any type. It must be the last (group of) parameters in a function signature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void printAll(std::string items, ...);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in order to process the arguments, you have to use a couple of macros defined in &lt;code&gt;&amp;lt;cstdarg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, we need &lt;code&gt;va_list&lt;/code&gt; to hold the information needed by the other macros. Then using &lt;code&gt;va_start()&lt;/code&gt; you get access to the first argument, then with &lt;code&gt;va_arg()&lt;/code&gt; you get access to each coming one and with &lt;code&gt;va_end()&lt;/code&gt; you finish the traversal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdarg&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;va_list&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;va_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;va_arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;va_end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;8.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The thing is, it's extremely error-prone. Only one out of the above 3 calls is correct.&lt;/p&gt;

&lt;p&gt;Let's see the outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 2 1 -665149312 
2 -665177168 24116274 
23 32 8 11 9 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first call, we first pass 4 indicating that we are passing 4 arguments and that's what we do, we pass overall four and four values are printed. While the first three are fine, the fourth one is a negative number. Well, the function reads some value from uncharted memory territories. We should have passed in &lt;code&gt;3&lt;/code&gt; as a count to show that we want to print 3 arguments. Sadly, the function didn't complain that we try to print more than was passed in.&lt;/p&gt;

&lt;p&gt;In the second case, we send in the right amount of parameters, but while the function tries to read out &lt;code&gt;int&lt;/code&gt; values, two of the inputs are floating point numbers. And while you might have expected that those numbers are truncated to integers, instead some odd values are printed. The reason is that an integer and a floating point number are represented differently in memory so when you try to read an &lt;code&gt;int&lt;/code&gt; as a &lt;code&gt;float&lt;/code&gt; or a &lt;code&gt;float&lt;/code&gt; as an &lt;code&gt;int&lt;/code&gt;, you'll end up with something completely different. Though I don't understand why the value &lt;code&gt;2&lt;/code&gt; appears in the first place, instead of the second.&lt;/p&gt;

&lt;p&gt;The third call is fine. We pass in the right amount of parameters and of the right type. But these short examples already showcased how easy it is to shoot ourselves in the leg with variadic functions. It's one thing that they rely on macros, which is clearly not the way in 2022, but they are also entirely unsafe and they rely on that you count the number of passed-in arguments.&lt;/p&gt;

&lt;p&gt;Now let's see if variadic templates are safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are variadic templates?
&lt;/h2&gt;

&lt;p&gt;What is going to be similar is the use of the three dots (or ellipses): &lt;code&gt;...&lt;/code&gt;. But where should those dots appear?&lt;/p&gt;

&lt;p&gt;They can both appear before and after the type parameter! Depending on where they appear, they have different meanings. So far, so bad!&lt;/p&gt;

&lt;p&gt;Let's have a look at the implementation of our &lt;code&gt;printAll&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdarg&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;...);&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"duck"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You notice immediately that we have 2 overloads. One for the normal case and one for the variadic one. The first one prints one argument, while the second one recursively calls the first as it expands its parameter pack.&lt;/p&gt;

&lt;p&gt;Let's talk about two things here. First, the position of the ellipses and second a bug in our implementation and how to fix it.&lt;/p&gt;

&lt;p&gt;In the template parameter list, there are three dots between the &lt;code&gt;typename&lt;/code&gt; keyword and the name of the template parameter pack (&lt;code&gt;Args&lt;/code&gt;). That's by convention that it's attached to the &lt;code&gt;typename&lt;/code&gt; but they can be attached to the parameter (&lt;code&gt;...Args&lt;/code&gt;) or they can be standalone as longs as the dots are consecutive (&lt;code&gt;typename ... Args&lt;/code&gt;), the compiler does not care.&lt;/p&gt;

&lt;p&gt;It's similar to the function parameter list. You can put the ellipses wherever you want between the parameter type and the parameter name. Again, by convention, we attach them to the parameter name meaning that there will be many of them coming.&lt;/p&gt;

&lt;p&gt;In the implementation, the three dots must follow the parameters, it does not matter whether there is a space in between or not. But if the &lt;code&gt;...&lt;/code&gt; are missing, in other words, if you don't unpack the parameter pack, the compilation fails. As long as there is more than one parameter in the pack, the same variadic overload is called recursively and once there is only one left, the other overload is pulled that will stop the recursion.&lt;/p&gt;

&lt;p&gt;And now let's talk about the problem, which does not come up for example if you want to sum up a list of numbers, but I still decided to keep this example. We might learn something interesting.&lt;/p&gt;

&lt;p&gt;With this version, We are printing &lt;em&gt;n-2&lt;/em&gt; newlines after calling &lt;code&gt;printAll&lt;/code&gt; where n is the number of parameters to be printed. One way to overcome this issue would be to have a parameter for indicating whether a new line should be printed at the end of the call, and it would be only &lt;code&gt;true&lt;/code&gt; for the client &lt;code&gt;printAll()&lt;/code&gt; call. To make the client pass in a boolean (or any other parameter) all the time would not be nice. It makes the API more difficult to use and also error-prone. Ideally, that could be done with a default argument, but both a default argument and a variadic template argument need to have the last place. Even though &lt;a href="https://stackoverflow.com/questions/14805192/c-variadic-template-function-parameter-with-default-value"&gt;there are some techniques to overcome this&lt;/a&gt; they are too complex to keep our code clean.&lt;/p&gt;

&lt;p&gt;We need a helper method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printAllImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;printAllImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Args&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printAllImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAllImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;...);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printAllImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"duck"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
3 2 1 
8.2 2 1.1 duck 
23 32 8 11 9 
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the separation of &lt;code&gt;printAllImpl&lt;/code&gt; from the non-recurisve &lt;code&gt;printAll&lt;/code&gt; we achieved that there will only be one newline character printed and we used perfect forwarding so that we reduce the unnecessary copies.&lt;/p&gt;

&lt;p&gt;This solution is totally superior to the variadic function both in terms of readability and in terms of type safety.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we discussed the differences between variadic functions and variadic templates. We saw how to use variadic functions, how they rely on macros and that we pass in exactly the types a variadic function expects and also the right amount of them. Variadic templates are easier to read and easier to use. They provide the type safety that is missing from the old variadic functions.&lt;/p&gt;

&lt;p&gt;Do you still use variadic functions? If so what are your arguments?&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>templates</category>
      <category>variadic</category>
      <category>tmp</category>
    </item>
    <item>
      <title>Without RTTI your code will be cleaner</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 26 Apr 2023 12:40:26 +0000</pubDate>
      <link>https://dev.to/sandordargo/without-rtti-your-code-will-be-cleaner-12oe</link>
      <guid>https://dev.to/sandordargo/without-rtti-your-code-will-be-cleaner-12oe</guid>
      <description>&lt;p&gt;Recently, in the binary sizes series, we discussed &lt;a href="https://www.sandordargo.com/blog/2023/03/01/binary-sizes-and-rtti"&gt;how run-time type information affects RTTI&lt;/a&gt;. I also mentioned that in my opinion the lack of RTTI leads to better practices and you'll end up with a more readable, more maintainable code.&lt;/p&gt;

&lt;p&gt;It's time to delve into this topic and see why.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RTTI again?
&lt;/h2&gt;

&lt;p&gt;But first of all, let's quickly recap what is run-time type information. RTTI let us have information on the dynamic type of reference/pointer types. It lets us use &lt;code&gt;dyanamic_cast&lt;/code&gt;s and also call the &lt;code&gt;typeid()&lt;/code&gt; function and query the returned instance of &lt;code&gt;std::type_info&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I think that not having access to these tools will let you write better code. Why so?&lt;/p&gt;

&lt;p&gt;Let's start with probably the less controversial tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't rely on &lt;code&gt;typeid()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Inexperienced programmers might want to automatically use &lt;code&gt;typeid().name()&lt;/code&gt; to see what's the dynamic type of something during development and testing. It's usually not kept because often the output is not that readable. I mean for &lt;code&gt;int&lt;/code&gt;, you might simply get &lt;code&gt;i&lt;/code&gt;. That's something easily recognizable, but not something you'd want to see in the logs.&lt;/p&gt;

&lt;p&gt;The bigger problem is that the output is implementation-defined, so you really shouldn't count on it if you want code that is portable. (Even though there is a fair chance that they will be the same on GCC and clang).&lt;/p&gt;

&lt;p&gt;Let's see a couple of examples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5u&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'5'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*
gcc:   i j d b c
clang: i j d b c
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Different outputs among compilers do not mean that you cannot use them consistently. You can still use &lt;code&gt;typeid().name()&lt;/code&gt; or &lt;code&gt;typeid.hash()&lt;/code&gt; to compare types against each other in your code and branch the execution based on those comparisons...&lt;/p&gt;

&lt;p&gt;On the other hand, you don't have many reasons to do that. If you have to deal with, if you have to compare unrelated types, probably you should extract the type-dependent code and use different overloads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Wine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Wine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"this is wine&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WhiteWine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;WhiteWine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"this is white wine&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RedWine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;RedWine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"this is red wine&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RoseWine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;RoseWine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"this is rose wine&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;wines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;wine1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WhiteWine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;wine2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RedWine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;wine3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoseWine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the types are from the same inheritance tree, then you should either use &lt;code&gt;dynamic_cast&lt;/code&gt;s or even better, you should benefit from dynamic dispatching.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;dynamic_cast&lt;/code&gt; is slightly better, but still should be avoided
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;dynamic_cast&lt;/code&gt; safely converts pointers and references to classes up, down and sideways along the inheritance hierarchy&lt;/em&gt; - according to &lt;a href="https://en.cppreference.com/w/cpp/language/dynamic_cast"&gt;CppReference&lt;/a&gt;. Often when you have a collection of pointers to the base class, you'll try to cast it to different derived classes and if the case is successful you do whatever you want with that type.&lt;/p&gt;

&lt;p&gt;In other words, when you have no idea what types you have, you can start casting things. You might even have a series of casts like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;OffRoader&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;offroader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OffRoader&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offroader&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;offroader&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;turnOnAllWheelDrive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Van&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;van&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Van&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;van&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;van&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attachThirdSeatRow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Roadster&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;roadster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Roadster&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;roadster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;roadster&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;removeRoof&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Many would say that this is a code smell. I'm among them. Many would go further and say that using &lt;code&gt;dynamic_cast&lt;/code&gt; in general is a code smell.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, this is almost always better than relying on &lt;code&gt;typeid()&lt;/code&gt;, but behind &lt;code&gt;dyanimc_cast&lt;/code&gt;s you'll only find bad inheritance trees and messed up APIs.&lt;/p&gt;

&lt;p&gt;Rather than having different public interfaces, we should have a unified one.&lt;/p&gt;

&lt;p&gt;There is a convenient reason for that. If the interface is unified, we don't need to cast our objects, we can simply rely on runtime dispatching of our function calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doSomeFun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OffRoader&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doSomeFun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"use all wheel drive on OffRoader&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Van&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doSomeFun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"attach third seatrow in a van&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Roadster&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;doSomeFun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"remove roadster's roof&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myCars&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;myCars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OffRoader&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;myCars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Van&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;myCars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Roadster&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;myCars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;doSomeFun&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When &lt;code&gt;typeid&lt;/code&gt; is still better?
&lt;/h2&gt;

&lt;p&gt;As I mentioned earlier, while &lt;code&gt;dynamic_cast&lt;/code&gt; is not a good solution, it's almost always better than using &lt;code&gt;typeid()&lt;/code&gt;. Why not always?&lt;/p&gt;

&lt;p&gt;There is a not-so-subtle difference between these 2 RTTI tools. &lt;code&gt;dynamic_cast&lt;/code&gt; checks whether an object "is kind of" another class. But &lt;code&gt;typeid()&lt;/code&gt; will look for an exact match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;dynamic_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"p1 is like B*. it's convertible to B&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// we won't reach this branch due to bad ordering of if/else branches&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"p1 is like C*. it's convertible to C&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;{}))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"*p1 is B&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;{}))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"*p1 is C&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/*
p1 is like B*. it's convertible to B
*p1 is C
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As such, &lt;code&gt;typeid()&lt;/code&gt; is both simpler and faster. Even though we had to construct an extra instance of &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; in the above example to be able to compare the type information. If that's costly and/or we must do this several times, we could create a map of &lt;code&gt;std::type_index&lt;/code&gt; objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;type_index&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;typeMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;type_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;{}))},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;type_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;{}))},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;type_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;{}))}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"p1 is like B*. it's convertible to B&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// we won't reach this branch due to bad ordering of if/else branches&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"p1 is like C*. it's convertible to C&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;typeMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"*p1 is B&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;typeMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"*p1 is C&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a couple of things to note:&lt;br&gt;
&lt;del&gt;- &lt;code&gt;typeMap&lt;/code&gt; is &lt;code&gt;const&lt;/code&gt;. It's necessary, because &lt;code&gt;std::type_index&lt;/code&gt; is a copyable wrapper around the non-copyable &lt;code&gt;std::type_info&lt;/code&gt;. It is not default constructible. In other words, if the &lt;code&gt;map&lt;/code&gt; is not &lt;code&gt;const&lt;/code&gt;, then the code wouldn't compile as a mutable map's value type must be default constructible.&lt;/del&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;std::type_index&lt;/code&gt; is a copyable wrapper around the non-copyable &lt;code&gt;std::type_info&lt;/code&gt;. As &lt;code&gt;std::type_index&lt;/code&gt; does not have a default constructor, you cannot use &lt;code&gt;operator=()&lt;/code&gt; to add new items to the map. You either initialize it at declaration (which is preferable), or you use the &lt;code&gt;insert()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;We use &lt;code&gt;map::at()&lt;/code&gt; and cannot use &lt;code&gt;map::operator[]&lt;/code&gt;. The reason is that &lt;code&gt;map::operator[]&lt;/code&gt; is not &lt;code&gt;const&lt;/code&gt; whereas &lt;code&gt;map::at()&lt;/code&gt; has a &lt;code&gt;const&lt;/code&gt; overload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, the best is still avoiding both using &lt;code&gt;typeid&lt;/code&gt; and &lt;code&gt;dynamic_cast&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to still use &lt;code&gt;dyanmic_cast&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The C++ core guidelines explain well the differences between casting to a pointer or a reference type and when you should refer which (&lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c147-use-dynamic_cast-to-a-reference-type-when-failure-to-find-the-required-class-is-considered-an-error"&gt;C.147&lt;/a&gt; and &lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c148-use-dynamic_cast-to-a-pointer-type-when-failure-to-find-the-required-class-is-considered-a-valid-alternative"&gt;C.148&lt;/a&gt;), but it doesn't mean that you should use any.&lt;/p&gt;

&lt;p&gt;In fact, even &lt;a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c153-prefer-virtual-function-to-casting"&gt;C.153&lt;/a&gt; says that you should prefer using virtual functions to casting. Casting is error-prone and you can make mess it up easily. Virtual functions are safe and when you call virtual functions it's guaranteed that you'll reach the most derived function. On the other hand, we saw with &lt;code&gt;dynamic_cast&lt;/code&gt; that you might end up calling an intermediary function.&lt;/p&gt;

&lt;p&gt;As we saw earlier, with virtual functions your code will be cleaner and speed is not an issue because &lt;code&gt;dynamic_cast&lt;/code&gt; is (also) slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With Run-time Type Information, we get access to &lt;code&gt;typeid()&lt;/code&gt; and &lt;code&gt;dynamic_cast&lt;/code&gt;. They help us query the dynamic types of polymorphic objects. In other words, they help us identify which derived objects are held by base class pointers.&lt;/p&gt;

&lt;p&gt;With great power, we also get great responsibility. Sadly, experience shows that responsibility is often abused. These tools are often overused and result in messy code. Using &lt;code&gt;virtual&lt;/code&gt; functions and relying on dynamic dispatching is almost always better.&lt;/p&gt;

&lt;p&gt;Besides having cleaner code, you can also benefit from shorter compile times and smaller binaries as the type information doesn't have to be generated and stored. To me, turning RTTI off is a great option to consider.&lt;/p&gt;

&lt;p&gt;Do you rely on it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>rtti</category>
      <category>virtual</category>
      <category>dynamiccast</category>
    </item>
    <item>
      <title>We are always late</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 19 Apr 2023 07:20:32 +0000</pubDate>
      <link>https://dev.to/sandordargo/we-are-always-late-4na8</link>
      <guid>https://dev.to/sandordargo/we-are-always-late-4na8</guid>
      <description>&lt;p&gt;I recently read a post on social media about modern people who are always chasing themselves as they are always late. It also reminded me of what one of my first bosses told me. According to the mentioned post, we all live as if we were late all the time. We keep complaining that we have no time and wherever we are, whatever we are doing, we just keep thinking about the next thing to do. Therefore we are not present even in our own lives.&lt;/p&gt;

&lt;p&gt;If we are always late then we are anxious all the time. Being late is vastly different from waiting for something. While the latter is often positive - oh, I'm waiting for meeting my partner at this nice restaurant - the former is always negative - oh, I'm late from that bloody restaurant and my partner will be pissed off once again!&lt;/p&gt;

&lt;p&gt;I feel this too. I don't have enough time. Neither at work nor in my personal life.&lt;/p&gt;

&lt;p&gt;I have many ideas on what to do, but I simply don't have enough time to implement them. By now, I reached the point where I don't even have an idea how I could find significantly more time.&lt;/p&gt;

&lt;h2&gt;
  
  
  But is that normal?
&lt;/h2&gt;

&lt;p&gt;There is the other idea that Béla told me, one of my first managers. He said that I should not worry about finishing all my tasks. There will be always more work at a corporation than time to finish it.&lt;/p&gt;

&lt;p&gt;After 13 years of working for bigger companies, I'm sure that he was right. I don't remember a case where I would have had to think for more than a few seconds to find another task.&lt;/p&gt;

&lt;p&gt;But isn't this stressful?&lt;/p&gt;

&lt;p&gt;For many it is stressful because they think that they cannot stop, they should do whatever is lined up as soon as possible.&lt;/p&gt;

&lt;p&gt;Personally, I don't find this stressful at all.&lt;/p&gt;

&lt;p&gt;Let me explain why.&lt;/p&gt;

&lt;p&gt;First of all, I see it as a challenge and an opportunity to learn. If you have more work than time, you have to prioritize. Knowing how to prioritize is a valuable skill in all domains of your life. If you had more time than tasks, you'd not be able to learn this skill.&lt;/p&gt;

&lt;p&gt;Secondly, people are greedy. Including stakeholders. They always want more and more and they often want it for less and less. You shouldn't feel bad because you cannot satisfy greedy people's ideas.&lt;/p&gt;

&lt;p&gt;But it's not all that negative. The excess of tasks does not only come because of greed, it's also a sign of creativity and care. You and your team know how to improve the code, how to make your product better, hence you have a bigger backlog that needs prioritization. Then it's up to the product owner/manager to decide on the priorities and up to the team to see how many tasks they can take. But rest assured there will be more tasks than you can take on.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I deal with it in my personal life?
&lt;/h2&gt;

&lt;p&gt;In my own life, I'm both the product owner and the person who gets to implement the tasks. I have a chip in the game. I want to complete as many tasks as possible to get forward. I don't have a boss who understands that one cannot get everything done.&lt;/p&gt;

&lt;p&gt;Or to put it differently, I am the boss and I'm a demanding one - demanding towards myself.&lt;/p&gt;

&lt;p&gt;Does this mean that I'm always unhappy with my results, with my progress? Not exactly, I'm usually happy with my progress, I always try to see how far I got and not how far I'm still from my goals. But at the same time, I find it frustrating that I cannot spend more time on things that I'd like to do.&lt;/p&gt;

&lt;p&gt;Here is my biggest mental pain. I find it extremely difficult to accept that I have almost no personal time at all apart from the time that I allocate from my sleep time. I get up about 90 minutes earlier than the rest of the family and I try to stay up later than I would normally do to be able to spend time on writing, reading, and learning.&lt;/p&gt;

&lt;p&gt;Then I also feel humbled and grateful that at least I can manage this. There are many single and probably non-single parents who have to use even these parts of the day to provide. To do the chores, to cook for the next day, and so on.&lt;/p&gt;

&lt;p&gt;I try to think about Toni Morrison who as a divorced mother with 2 little kids and with a day job managed to write her first book in the late evenings.&lt;/p&gt;

&lt;p&gt;I know that I'm in a very good situation even considering my free time, but it's a hard job to remind myself about and accept that &lt;em&gt;this&lt;/em&gt; is good. &lt;em&gt;This&lt;/em&gt; is realistic.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I deal with it at work?
&lt;/h2&gt;

&lt;p&gt;At work, I have understood what are realistic expectations and I do my best to live up to them. I understand that we cannot do everything and that we sell our time to our employer and we should do our best during the contracted time and stop when it ends.&lt;/p&gt;

&lt;p&gt;It doesn't mean it's easy to stop. If you like your job, if you like what you're doing, then it's difficult to stop. Sometimes it's difficult for me to stop, but I remind myself that there are other things in life. Actually, many other things that I cannot complete. &lt;/p&gt;

&lt;p&gt;This helps.&lt;/p&gt;

&lt;p&gt;But that's only one side of the story. The other side is that I regularly talk about what can be done, and how much I can do with my manager and/or with my peers so that we are on the same page. If I get a new task and I feel that it's getting too much, I remind the manager or PO about it and I ask what should be deprioritized from the other tasks. I usually propose a few to choose from. Most of the management I've worked with understood this and picked something, sometimes the task once understood that it's not so much important.&lt;/p&gt;

&lt;p&gt;When it comes to the quantity of work, it's worth keeping in mind two things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The average office worker spends around 3 hours a day at productive work&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sandordargo.com/blog/2022/08/03/prices-law"&gt;50% of the work is done by the square root of the total number of people who participate in the work&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Try to aim to be somewhere in between the two groups. Even though the 3 hours might be higher in certain companies. I think it's higher at Spotify, partly because you can survive with fewer meetings (most of the time), so you have more time to work.&lt;/p&gt;

&lt;p&gt;At the same time, I think it's not worth being in the other group. Those people are not paid that much better, but they work a lot more. I'd say that the value of extra work is diminishing.&lt;/p&gt;

&lt;p&gt;It might seem ridiculous to think about these things, but it helps set both your goals and boundaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the difference?
&lt;/h2&gt;

&lt;p&gt;And that's why most people feel stressed about their long list of tasks. Most of us are not clear on our boundaries and therefore we are uncomfortable saying no. Partly because we are afraid that by saying no, we might be dismissed and also because we see it as a personal failure.&lt;/p&gt;

&lt;p&gt;Managing this is difficult. I cannot do it well in my private life. I essentially sleep less to be able to do a bit more, but at least I know my limits. I know what's the point that if I bypass, I won't be able to get up on time and I'll feel shit the whole day. Even though I respect that in most cases, I don't think I have enough sleep time.&lt;/p&gt;

&lt;p&gt;Work is easier because I realise I don't work for myself there. Don't get me wrong. I put in a fair quality and quantity of work. I don't think that I take advantage of the company or that the company takes advantage of me.&lt;/p&gt;

&lt;p&gt;I don't feel bad that I could still continue working, that I still have many to do at the end of the day. But&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if I work more, I won't get paid more&lt;/li&gt;
&lt;li&gt;if I work more, it won't make a big difference on the scale of the company&lt;/li&gt;
&lt;li&gt;if I work more, my family will feel it and it'll have bad consequences for us.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key is that you have to understand your limits and also to communicate those limits with your peers and your managers so that they won't have false expectations towards you. This is so important. It's better to get 10 units if you communicate 10 than get 15 if you set the expectation to 20. In the latter case, you'll likely feel exhausted and disappointed. The others will simply be disappointed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"I need good people not tonight, but also in a year. If you keep working like that, you'll burn out. Please, go home now."&lt;/em&gt; - This is what one of my former senior managers used to tell a colleague who always stayed late and worked overtime. He didn't only care about the product, but also about the people. We loved him so much for a reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Today we discussed the relationship between available time and work to do. The latter is always more abundant than the former. If not, I think your job is at stake and you have other problems to focus on.&lt;/p&gt;

&lt;p&gt;My advice is that don't overwork and do not feel late or bad about the work lined up. It's not your fault that there is more work than time. It's natural at the companies. If you're a developer and you're stressed out because of this, you are probably not shielded enough and your product owner is not setting priorities well.&lt;/p&gt;

&lt;p&gt;In your personal life, it's more difficult to set the right boundaries and not to feel bad, but try to remind yourself about your professional approach and maybe you find some help and reassurance in that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>watercooler</category>
      <category>career</category>
      <category>selfmanagement</category>
      <category>timemanagement</category>
    </item>
    <item>
      <title>Vectors and unique pointers</title>
      <dc:creator>Sandor Dargo</dc:creator>
      <pubDate>Wed, 12 Apr 2023 06:20:56 +0000</pubDate>
      <link>https://dev.to/sandordargo/vectors-and-unique-pointers-55eb</link>
      <guid>https://dev.to/sandordargo/vectors-and-unique-pointers-55eb</guid>
      <description>&lt;p&gt;In this post, I want to share some struggles I had twice during the last few months. For one of my examples, I wanted to initialize a &lt;code&gt;std::vector&lt;/code&gt; with &lt;code&gt;std::unique_ptr&lt;/code&gt;. It didn't compile and I had little time, I didn't even think about it. I waved my hand and changed my example.&lt;/p&gt;

&lt;p&gt;Then I ran into the same issue at work, while we were pairing over an issue. I couldn't just wave anymore, and luckily we also had the time to go a bit deeper.&lt;/p&gt;

&lt;p&gt;What we wanted was a little bit different and more complex though. We wanted to return a vector of unique pointers to a struct that holds - among others - a unique pointer.&lt;/p&gt;

&lt;p&gt;This is a simplified version of what we wanted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// unimportant&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// somewhere in a function&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aName&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anotherName&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a bit too complex to start with, so let's dissect the issue into two parts.&lt;/p&gt;

&lt;h2&gt;
  
  
  No compiler-generated copy constructor
&lt;/h2&gt;

&lt;p&gt;Let's first omit the external unique pointer and try to brace-initialize a &lt;code&gt;vector&lt;/code&gt; of &lt;code&gt;Wrapper&lt;/code&gt; objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// unimportant&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;aName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"bla"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;aName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()}};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first part of the problem is that we cannot &lt;code&gt;{}&lt;/code&gt;-initialize this &lt;code&gt;vector&lt;/code&gt; of &lt;code&gt;Wrapper&lt;/code&gt;s. Even though it seems alright at a first glance. &lt;code&gt;Wrapper&lt;/code&gt; is a &lt;code&gt;struct&lt;/code&gt; with public members and no explicitly defined special functions. Our &lt;code&gt;{}&lt;/code&gt;-initialization follows the right syntax and the parameters are passed in the right order.&lt;/p&gt;

&lt;p&gt;Still, the compiler says stop! Here is a part of the error messages. As &lt;code&gt;vector&lt;/code&gt; is a template we cannot expect a concise message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_uninitialized.h:90:56: error: static assertion failed: result type must be constructible from input type
   90 |       static_assert(is_constructible&amp;lt;_ValueType, _Tp&amp;gt;::value,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we scroll up in the errors we'll find a more explanatory line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In instantiation of 'constexpr bool std::__check_constructible() [with _ValueType = Wrapper; _Tp = const Wrapper&amp;amp;]':
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the problem is &lt;code&gt;Wrapper&lt;/code&gt; cannot be constructed from &lt;code&gt;const Wrapper&amp;amp;&lt;/code&gt;, in other words, &lt;code&gt;Wrapper&lt;/code&gt; cannot be copy constructed. That makes sense! It has a &lt;em&gt;move-only&lt;/em&gt; member, &lt;code&gt;std::unique_ptr&amp;lt;Resource&amp;gt; m_resource&lt;/code&gt;! Because of this &lt;em&gt;move-only&lt;/em&gt; member, the compiler cannot automatically generate a copy constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  A &lt;code&gt;std::vector&lt;/code&gt; always copies its &lt;code&gt;std::initializer_list&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;That's all fine but why do we need a copy constructor? Why cannot we benefit from move semantics?&lt;/p&gt;

&lt;p&gt;We can spot the answer on &lt;a href="https://en.cppreference.com/w/cpp/container/vector/vector"&gt;C++ Reference&lt;/a&gt;! &lt;code&gt;std::vector&lt;/code&gt; has only one constructor involving a &lt;code&gt;std::initializer_list&lt;/code&gt; and there the &lt;code&gt;initializer_list&lt;/code&gt; is taken by value. In other words, &lt;code&gt;vector&lt;/code&gt; copies its &lt;code&gt;initializer_list&lt;/code&gt;. Always.&lt;/p&gt;

&lt;p&gt;As the passed in &lt;code&gt;initializer_list&lt;/code&gt; is going to be copied, the contained type must be copy-constructible. That's clearly not the case for &lt;code&gt;Wrapper&lt;/code&gt; which we can easily assert.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;static_assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_copy_constructible&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="cm"&gt;/*
main.cpp:18:20: error: static assertion failed
   18 | static_assert(std::is_copy_constructible&amp;lt;Wrapper&amp;gt;());
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Let's make contained types copy constructible
&lt;/h3&gt;

&lt;p&gt;That's quite easy to fix, we need to provide a user-defined copy constructor, such as &lt;code&gt;Wrapper(const Wrapper&amp;amp; other): m_name(other.m_name), m_resource(std::make_unique&amp;lt;Resource&amp;gt;()) {}&lt;/code&gt;. At the same time, let's not forget about &lt;a href="https://en.cppreference.com/w/cpp/language/rule_of_three"&gt;the rules of 0/3/5&lt;/a&gt;, so we should provide all the special functions.&lt;/p&gt;

&lt;p&gt;Now we don't have the problem anymore, we can easily use the brace initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// unimportant&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;m_resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;aName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"bla"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;aName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()}};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if we cannot or do not want to modify &lt;code&gt;Wrapper&lt;/code&gt;? Do we have any other options?&lt;/p&gt;

&lt;p&gt;Of course!&lt;/p&gt;

&lt;h3&gt;
  
  
  We can also extract &lt;code&gt;vector&lt;/code&gt; initialization
&lt;/h3&gt;

&lt;p&gt;If we have to avoid the brace-initialization of the vector, we can simply default initialize it, probably reserving then enough space for the items we want to add, and use either &lt;code&gt;vector&lt;/code&gt;'s &lt;code&gt;emplace_back()&lt;/code&gt; or &lt;code&gt;push_back()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;There is not a big difference in this case between &lt;code&gt;emplace_back()&lt;/code&gt; and &lt;code&gt;push_back()&lt;/code&gt;. &lt;code&gt;push_back()&lt;/code&gt; will call the appropriate constructor first and then the move constructor. &lt;code&gt;emplace_back()&lt;/code&gt; will only make one constructor call. At least that's the theory. Let's see if we're right. I amended &lt;code&gt;Wrapper&lt;/code&gt; so that each of its special functions prints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// unimportant&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper()&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"~Wrapper()&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper (std::string name, std::unique_ptr&amp;lt;Resource&amp;gt; resource)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper (const Wrapper&amp;amp; other)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper (Wrapper&amp;amp;&amp;amp; other)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper&amp;amp; operator=(const Wrapper&amp;amp; other)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Wrapper&amp;amp; operator=(Wrapper&amp;amp;&amp;amp; other)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;aName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"bla"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="c1"&gt;// v.push_back(Wrapper(aName, std::make_unique&amp;lt;Resource&amp;gt;()));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/*
output with emplace_back:
Wrapper (std::string name, std::unique_ptr&amp;lt;Resource&amp;gt; resource)
~Wrapper()
*/&lt;/span&gt;

&lt;span class="cm"&gt;/*
output with push_back
Wrapper (std::string name, std::unique_ptr&amp;lt;Resource&amp;gt; resource)
Wrapper (Wrapper&amp;amp;&amp;amp; other)
~Wrapper()
~Wrapper()
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our assumption was right, except that I forgot to note the extra destructor call on a moved-from object. While these extra calls would be often negligible, we have no reason not to use &lt;code&gt;emplace_back()&lt;/code&gt;. It's not more difficult to write it and it's better for the performance.&lt;/p&gt;

&lt;p&gt;With that, we've seen why we couldn't &lt;code&gt;{}&lt;/code&gt;-initialize a &lt;code&gt;vector&lt;/code&gt; of &lt;code&gt;Wrapper&lt;/code&gt;s following the &lt;a href="https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero"&gt;rule of zero&lt;/a&gt;, where &lt;code&gt;Wrapper&lt;/code&gt; is a type that allocates on the heap.&lt;/p&gt;

&lt;p&gt;Now let's move over to the other issue.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same issue with &lt;code&gt;initializer_list&lt;/code&gt; once again
&lt;/h2&gt;

&lt;p&gt;Actually, by answering the first question, we also learned why we cannot &lt;code&gt;{}&lt;/code&gt;-initialize a &lt;code&gt;vector&lt;/code&gt; of &lt;code&gt;unique_ptr&lt;/code&gt;s. It's the problem of an &lt;code&gt;initializer_list&lt;/code&gt; taken by value instead of reference and the fact that &lt;code&gt;unique_ptr&lt;/code&gt;s cannot be copied.&lt;/p&gt;

&lt;p&gt;Can we do anything to improve the situation?&lt;/p&gt;

&lt;p&gt;We can extract the creation and population of the &lt;code&gt;vector&lt;/code&gt; to a separate method so that we don't have the visual noise of &lt;code&gt;vector&lt;/code&gt; insertions along with the rest of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;createResources&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createResources&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But can we do something better?&lt;/p&gt;

&lt;p&gt;We can make a better, generalized function that makes us a &lt;code&gt;vector&lt;/code&gt; of &lt;code&gt;unique_ptr&lt;/code&gt;s, but the idea behind is essentially the same: the pointers are added one by one after the construction of the vector.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.cppstories.com/2023/initializer_list_improvements/"&gt;Let me borrow an implementation by Bartek&lt;/a&gt;. I take this piece of code from &lt;a href="https://www.cppstories.com/2023/initializer_list_improvements/"&gt;C++ Storties&lt;/a&gt; and I encourage you to read the whole article on &lt;code&gt;initializer_list&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="nf"&gt;makeVector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can update our original example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// unimportant&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Wrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;m_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;m_resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// this is from Bartek: https://www.cppstories.com/2023/initializer_list_improvements/&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;makeVector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;maybe_unused&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;makeVector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"keyboard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"mouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"screen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Today, I shared with you a problem I faced lately. I wanted to &lt;code&gt;{}&lt;/code&gt;-initialize a &lt;code&gt;vector&lt;/code&gt; of unique pointers, but it didn't work. A &lt;code&gt;std::vector&lt;/code&gt; takes an &lt;code&gt;initializer_list&lt;/code&gt; by value, so it makes a copy of it. Hence, the compilation will fail if you try to use an &lt;code&gt;initializer_list&lt;/code&gt; with move-only types.&lt;/p&gt;

&lt;p&gt;If you want to use the &lt;code&gt;{}&lt;/code&gt;-initializer for a vector, you need to implement the move constructor. If that's not an option and you want to separate the creation of the &lt;code&gt;vector&lt;/code&gt;, you have no other option than move the related code to a separate function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect deeper
&lt;/h2&gt;

&lt;p&gt;If you liked this article, please &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit on the like button,
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://eepurl.com/gvcv1j"&gt;subscribe to my newsletter&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;and let's connect on &lt;a href="https://twitter.com/SandorDargo"&gt;Twitter&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>vector</category>
      <category>movesemantics</category>
      <category>uniqueptr</category>
    </item>
  </channel>
</rss>
