<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>m42e's blog</title>
    <description>Blog about software development</description>
    <link>https://m42e.de</link>
    <atom:link href="https://m42e.de/feed.xml" rel="self" type="application/rss+xml" />
    <author>
      <name>Matthias Bilger</name>
      <email>matthias@bilger.info</email>
      <uri></uri>
    </author>
    
      <item>
        <title>unique_ptr are great</title>
        <description>&lt;p&gt;Sidenote: Ok, so I couldn’t and will not try to stick to a plan of posting, I will post if I am in the mood of telling somebody something.&lt;/p&gt;

&lt;p&gt;I recently got in touch with rust, and read about it. Some things look nice, others rather strange. And a few of the “oh this is much better in C++” maybe true, others are just not so obvious.
One special case I recently stumbled over was file access. You open a file, write to it and at some point int time you are closing it.&lt;/p&gt;

&lt;p&gt;Well if you remember to close it, right. Maybe you forget about it, so you have it open, even if it is not used anymore. That’s bad. But can be avoided. And C++ offers you a nice and clean way of doing it:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;writeToFileUsingUniquePtr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test2.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;w+&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;11234&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;check open files you see test2.txt threr&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;this_thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep_for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chrono&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;could not create/access file test2.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first line in the function is the most interesting here. You create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unique_ptr&lt;/code&gt; to a file with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fclose&lt;/code&gt; as deleter. I think that is so nice and easy that it is probably the only correct way to do it.&lt;/p&gt;

</description>
        <pubDate>Mon, 30 Aug 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/08/30/unique-ptr.html</link>
        <link href="https://m42e.de/2021/08/30/unique-ptr.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/08/30/unique-ptr.html</guid>
      </item>
    
      <item>
        <title>Ignore the nodiscard</title>
        <description>&lt;p&gt;I have recently decided to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[nodiscard]]&lt;/code&gt; for every function that returns something. Every may have some exceptions but in general all normal every-day functions are defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[nodiscard]]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This has two reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If there is a return value, it should mean something, so you should actively decide what to do.&lt;/li&gt;
  &lt;li&gt;If the return value is not useful at all, remove it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;non-owned-code&quot;&gt;Non owned code&lt;/h2&gt;

&lt;p&gt;So far so good, the rule is nice and works well, but in our code base we have to deal with lots of third party code we cannot change, at least not easily. I think we all know the struggle.
Additionally to the compiler raising errors on each ignored return value flagged with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[nodiscard]]&lt;/code&gt; we have some static code checkers which perform this check for all the function calls.&lt;/p&gt;

&lt;p&gt;We have a third party interface which has a return value, but it is not useful for us. So we want to ignore it. Simply discard it with not at least some cast does not work, as the mentioned code checker would flag it as an issue.&lt;/p&gt;

&lt;p&gt;Thankfully we get something within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std&lt;/code&gt; even if it is not exactly where you might expect it. You have to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#include &amp;lt;tuple&amp;gt;&lt;/code&gt;. And then you get a small but useful gem. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::ignore&lt;/code&gt;. 
Remember the old days, where you did something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(void) function_with_return()&lt;/code&gt; or a more C++ style &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static_cast&amp;lt;void&amp;gt;(function_with_return())&lt;/code&gt;. These times are gone. From now on you can simply state what you want to do:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;tuple&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;function_with_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ignore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_with_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This makes it pretty clear you are not interested in the value receiving as a return. I like it pretty much as it solves another inconvenience we dealt way to long.&lt;/p&gt;
</description>
        <pubDate>Wed, 09 Jun 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/06/09/nodiscard-ignore.html</link>
        <link href="https://m42e.de/2021/06/09/nodiscard-ignore.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/06/09/nodiscard-ignore.html</guid>
      </item>
    
      <item>
        <title>Get rid of unions and some more advantages of std::variant.</title>
        <description>&lt;p&gt;Let’s go back to C++17. It is already a while out there and a feature I really like is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt;.
It is handy in a lot of places. In all places you may have used unions before. Having an Interface which accepts a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; which it does not use itself, but passes it further makes the interface slimmer and easier. 
You could have done that with templates, but using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; is much more simple.&lt;/p&gt;

&lt;p&gt;Let’s look at some code.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;variant&amp;gt;
#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;literals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;funcb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;a string, how nice: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;not a string, something else&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;size is &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;funcb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;AString&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3208&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

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

&lt;p&gt;Ok, so we are able to pass things without bothering. I mean in this case it has been easy, we have a string and an integer. So is there another way than these &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::get_if&lt;/code&gt;? Indeed. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt; in combination with lambdas is really great. You are using templates without a single template parameter. It is so nice, elegant and not too hard.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;variant&amp;gt;
#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;literals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;size is &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_same_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;int &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_same_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;string &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;AString&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3208&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

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

&lt;p&gt;The lambda in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt; is using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constexpr&lt;/code&gt;. This will reduce the generated functions to a minimum. The lamba here act like a template and we do a specialization there so it handles all types correctly.
I found this pretty useful also for some kind of serialization. So you have it all in one place.&lt;/p&gt;

</description>
        <pubDate>Sun, 06 Jun 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/06/06/visit.html</link>
        <link href="https://m42e.de/2021/06/06/visit.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/06/06/visit.html</guid>
      </item>
    
      <item>
        <title>That did not start well. Missed already some weeks...</title>
        <description>&lt;p&gt;Ok, here we are. I already missed some weeks, but hey, don’t mind. I wanted to challenge myself into writing and now I have to possibilities:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ignore the fact, that I missed already some weeks posts, or&lt;/li&gt;
  &lt;li&gt;cancel the project completely because I missed my own expectations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And I have chosen the first one.&lt;/p&gt;

&lt;h2 id=&quot;i-would-have-done-this-different-in-the-past&quot;&gt;I would have done this different in the past.&lt;/h2&gt;

&lt;p&gt;Maybe I am not the only one, but for sure I would have acted different in the past. My favourite choice and of course the one that is a lot easier is the second one. But I do not want to cancel this. It is a good practice for me to write about things.&lt;/p&gt;

&lt;p&gt;I usually assume that everyone knows a lot more than I do, aka. imposter syndrome. I mean I think of myself that I know pretty much, and from pretty broad topics, too. But still I am not confident, in a lot of areas. This is one thing that is hindering me from telling other people about what I am doing and how. I expect to bore them because for sure they already know it anyway.&lt;/p&gt;

&lt;p&gt;The decision to continue is a decision I am doing for myself. I do not expect anybody to read this here at all :)&lt;/p&gt;

&lt;p&gt;So enough insights, let us see what we have here this week.&lt;/p&gt;
</description>
        <pubDate>Sun, 06 Jun 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/06/06/learning.html</link>
        <link href="https://m42e.de/2021/06/06/learning.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/06/06/learning.html</guid>
      </item>
    
      <item>
        <title>Conecpts or what do you require?</title>
        <description>&lt;p&gt;Starting my regular blogging habit I do not want you to be bored out so I start with something that is new in C++20.
C++20 is another big step forward to make templates even better. Or some would say a step into the so called meta-programming. But do not be scared by the &lt;em&gt;meta&lt;/em&gt; it is not that complicated as you might have heard. For sure it can get complicated, but that is also true for non meta code.&lt;/p&gt;

&lt;p&gt;I will provide you some examples where concepts might be useful and a short reminder what templates are, just in case.&lt;/p&gt;

&lt;h2 id=&quot;short-reminder-templates&quot;&gt;Short reminder: templates&lt;/h2&gt;

&lt;p&gt;Just to make sure we are all on the same page, the basic thing we use templates for is as a cookie cutter. You do not buy a cookie cutter per dough and you do not want to repeat code.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This example is simple enough to show you what we can do with it. We have a &lt;strong&gt;function template&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; that takes two arguments of the same type, and applies the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; operator and return the value.&lt;/p&gt;

&lt;p&gt;So how to use it?&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.5&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;9.2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Simple isn’t it?&lt;/p&gt;

&lt;h2 id=&quot;so-why-bother-with-concepts&quot;&gt;So why bother with concepts?&lt;/h2&gt;

&lt;p&gt;One of the answers you might get from people which crawled through other peoples compiler error messages a lot might be: To get better error messages. This is true. You can try it yourself. Just pass a custom type to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; function (where the custom class does not implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator+&lt;/code&gt;). If you have never seen such error messages before you might be thrilled to get one :). Once more this example is so simple and easy, also the error message you get is understandable.&lt;/p&gt;

&lt;p&gt;Concepts are a good way to specify the template arguments your template accepts in a readable way and with better error messages.&lt;/p&gt;

&lt;h2 id=&quot;how-to-do-it&quot;&gt;How to do it?&lt;/h2&gt;

&lt;p&gt;It is not too hard. Besides this example there are a lot of predefined concepts you can use already available in the standard library.&lt;/p&gt;

&lt;p&gt;Let me show a concept for the add function above:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CompareableAddable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So what we require now is that the type passed has two things: an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator+&lt;/code&gt; and an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator==&lt;/code&gt;. 
&lt;strong&gt;Note:&lt;/strong&gt; Be aware that this does not mean, that it must have the same result. It just means the operation performed must be valid.&lt;/p&gt;

&lt;p&gt;You can play around here: &lt;a href=&quot;https://godbolt.org/z/3sooTaoTr&quot;&gt;https://godbolt.org/z/3sooTaoTr&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Sat, 15 May 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/05/15/concepts.html</link>
        <link href="https://m42e.de/2021/05/15/concepts.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/05/15/concepts.html</guid>
      </item>
    
      <item>
        <title>Try something new</title>
        <description>&lt;h2 id=&quot;this-is-my-new-challenge&quot;&gt;This is my new challenge&lt;/h2&gt;

&lt;p&gt;I try to write a blog post, once a week, with at least one C++ feature I either like or have learned about recently.
I take bets on how long I continue this.&lt;/p&gt;

&lt;p&gt;I know a lot of others are doing this and you all might be bored about just another blog like this. But if there is someone out there, who is interested do not hesitate to reach out and let me know about it.&lt;/p&gt;

&lt;p&gt;You can reach my using mail, &lt;a href=&quot;https://twitter.com/m42e_de&quot;&gt;twitter&lt;/a&gt; or &lt;a href=&quot;https://threema.id/SJPYUFBN&quot;&gt;Threema&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Sat, 08 May 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/05/08/try-something.html</link>
        <link href="https://m42e.de/2021/05/08/try-something.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/05/08/try-something.html</guid>
      </item>
    
      <item>
        <title>CPPnow 2021 - Lightning Talk</title>
        <description>&lt;p&gt;I had the opportunity to attend the &lt;a href=&quot;http://cppnow.org&quot;&gt;CPPnow&lt;/a&gt; in 2021. I am glad is has been a online conference, because that way I could attend more easily.&lt;/p&gt;

&lt;p&gt;Besides a lot of interesting, educating and inspiring talks I pushed myself forward and into doing a lightning talk. My first public talk somehow.&lt;/p&gt;

&lt;h2 id=&quot;what-i-learned&quot;&gt;What I learned&lt;/h2&gt;

&lt;p&gt;It was a nice experience. I think that I am actually less nervous than I thought I would. Thanks to the Team and the other speakers. I really enjoy presenting something. I hope I get the chance more often.&lt;/p&gt;

&lt;p&gt;Additionally I think my talk was far away from being perfect, but I recognized that does not matter. What matters is that I did do it.&lt;/p&gt;

&lt;p&gt;Maybe I hit onto somebody with the talk (which at some point in time will be available for sure). And even if not I learned something:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Presenting a talk isn’t as frightening as I thought&lt;/li&gt;
  &lt;li&gt;I can do it (at least for 5 minutes)&lt;/li&gt;
  &lt;li&gt;I think it is kind of funny to do&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;what-else&quot;&gt;What else&lt;/h2&gt;

&lt;p&gt;I really would like to do a longer talk somehow. But I am lacking of topics. Relevant topics, that are somehow interesting. But maybe this is a judgement I am doing to harsh?&lt;/p&gt;

&lt;p&gt;In my job programming roughly makes about 25%. And to be honest I am doing more paperwork and process fulfillment as I would like to. Maybe that is the reason I do not get too deep into topics. Maybe, but there might be something else.&lt;/p&gt;

&lt;p&gt;I try to deal with things as they are. So if there is not the right stuff available I will do the best I can to make things work. But I do that somehow careless. I do not make a big deal of it. I just wanted to build that stuff or to fix it. Maybe I should promote such things more.&lt;/p&gt;

&lt;p&gt;Finally I am getting bored really fast. As long as things are new and nice I like to play around with those. But I let go of them very fast if they are not of use or I see some fancy other stuff I like more. And this lets me abandon a lot of things I started. Mostly unfinished.&lt;/p&gt;

&lt;h2 id=&quot;include-c&quot;&gt;#include &amp;lt;C++&amp;gt;&lt;/h2&gt;

&lt;p&gt;I learned from the talk of Bryce Adelstein Lelbach that there is a movement to create a inclusive C++ community. I really like the idea and hope you support these movement, too. Details can be found here: &lt;a href=&quot;https://www.includecpp.org&quot;&gt;https://www.includecpp.org&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 08 May 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/05/08/lightning-talk.html</link>
        <link href="https://m42e.de/2021/05/08/lightning-talk.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/05/08/lightning-talk.html</guid>
      </item>
    
      <item>
        <title>Must Everything be a Smart Pointer?</title>
        <description>&lt;h2 id=&quot;tldr&quot;&gt;&lt;a href=&quot;#when-no-to-use&quot;&gt;&lt;/a&gt;TL;DR&lt;/h2&gt;

&lt;p&gt;If you pass a smart pointer you include some ownership, a raw pointer doesn’t. But use it for every allocation.&lt;/p&gt;

&lt;h2 id=&quot;should-we-only-use-smart-pointers&quot;&gt;&lt;a href=&quot;#only-smart-pointer&quot;&gt;&lt;/a&gt;Should we only use smart pointers?&lt;/h2&gt;

&lt;p&gt;During rewriting of a legacy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; codebase to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C++&lt;/code&gt; I have been asked if everything should be changed to a smart pointer. I replied with “Why should we do that?” and the response was “because we wanted to be modern”.&lt;/p&gt;

&lt;p&gt;Oh dear. That was not what I wanted to have by stating that we want to have a modern codebase. For sure use smart pointers, where you used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free&lt;/code&gt; in former times. But not everywhere and not under any circumstances.&lt;/p&gt;

&lt;p&gt;And here comes why. It has been explained for sure a few hundred times across the web, but I think it is worth explaining it one more time, so more people probably read it at some time.&lt;/p&gt;

&lt;h2 id=&quot;unique-pointer&quot;&gt;&lt;a href=&quot;#unique&quot;&gt;&lt;/a&gt;Unique Pointer&lt;/h2&gt;

&lt;p&gt;In case of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt; it seems to be a bit more obvious. This one clearly states, that there is only one of it, cannot be copied, only moved around. So most programmers are aware of that and use it with care. Did I say care? I mean they do not use it most of the time, because they cannot pass it to a function without moving it. But is that true? Well it depends.&lt;/p&gt;

&lt;p&gt;As &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt; does not have a copy or copy-assignment constructor you can only move it. That’s a fact. But there is this nice little method called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get&lt;/code&gt;. Now what do we get here? Lets have a look at a (simplified) full signature &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T* get() const noexcept;&lt;/code&gt;. Looks like we could easily access the pointer, and pass this to functions of our choice. Nice. But wait, I wanted to have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt; and not pass it around.&lt;/p&gt;

&lt;p&gt;Well I think you misunderstood the concept. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt; is meant to be unique in a way, that is has exactly &lt;strong&gt;one&lt;/strong&gt; owner at any point in time. And if the owner does not take care of the object referenced it will be deleted. The important part here is the owner.&lt;/p&gt;

&lt;h2 id=&quot;shared-pointer&quot;&gt;&lt;a href=&quot;#unique&quot;&gt;&lt;/a&gt;Shared Pointer&lt;/h2&gt;

&lt;p&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::shared_ptr&lt;/code&gt; describes a object that is shared. Sharing means it has &lt;strong&gt;at least one&lt;/strong&gt; owner but probably more. And these owners may leave the object alone at different times not synchronized. So the object must be there till the last one looses interest and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::shared_ptr&lt;/code&gt; gets destructed.&lt;/p&gt;

&lt;p&gt;Still, every owner could pass the raw pointer of the shared object around. The interface is like the one of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt;. And it should be used, as long as you do not want to &lt;strong&gt;share ownership&lt;/strong&gt;. If you calling a function which does something with the shared object: pass a raw pointer. If you construct an object, that has the same lifetime as your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::share_ptr&lt;/code&gt;: pass a raw pointer. If you want somebody else to have ownership (and therefore control over the lifetime): pass a shared_ptr.&lt;/p&gt;

&lt;h2 id=&quot;compared-to-things-in-real-life&quot;&gt;&lt;a href=&quot;#example&quot;&gt;&lt;/a&gt;Compared to things in real life&lt;/h2&gt;

&lt;p&gt;Lets explain with using something familiar, a cellphone number. Generally speaking there is only one owner of a cellphone number at a point in time. You may pass the mobile phone with the SIM card the number is bound, but then you do not have access to the number any more. So the SIM is kind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unique_ptr&lt;/code&gt;. (I know about multisim, but keep it simple). You still can pass your mobile phone to somebody else to make a call. It remains yours. And if you do not care anymore (your unique_ptr goes out of scope), the number is freed and you do not have to take care of it.&lt;/p&gt;

&lt;p&gt;On the other hand, lets assume you share a flat with two other people. The flat has a landline. This means, the landline is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::shared_ptr&lt;/code&gt;. All three of you kind of own the landline. As long as at least one of you care about it (own a shared_ptr) it won’t get canceled.&lt;/p&gt;

</description>
        <pubDate>Sat, 01 May 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/05/01/Must-Everything-be-a-Smart-Pointer.html</link>
        <link href="https://m42e.de/2021/05/01/Must-Everything-be-a-Smart-Pointer.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/05/01/Must-Everything-be-a-Smart-Pointer.html</guid>
      </item>
    
      <item>
        <title>What is Most Important when Porting Legacy Software?</title>
        <description>&lt;p&gt;I am currently working on a code base which is rather old an used to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt;. It has been started over a decade ago and is still in use. As part of an automotive ECU it has seen a number of base systems it has been integrated to. If you are familiar with automotive software development you may know &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AUTOSAR&lt;/code&gt;, a kind of extra layer above the OS which should make software components portable between and independent of the actual chip or event ECU it is running on.&lt;/p&gt;

&lt;p&gt;So the software we are currently porting fulfills the requirements and is bound to the constraints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AUTOSAR&lt;/code&gt; requires. Additionally, as it has existed before, it has even more legacy interfaces.&lt;/p&gt;

&lt;p&gt;The target is to port things to a modern POSIX compliant platform. And to make it more modern, to C++. C++17 to be a bit more precise. This is a great opportunity in my point of view. Get rid of all the old things that had to be supported, clean up interfaces and use the standard library. But most important: make the code more readable, understandable and maintainable.&lt;/p&gt;

&lt;p&gt;To achieve these three things I currently see one thing as crucial: &lt;strong&gt;Make the code descriptive&lt;/strong&gt;. Well, to not just give the thing another name, let me explain. I would define descriptiveness as a metric of how many lines of code do i have to read (or even worse, scroll) to determine the meaning of a single line of code.&lt;/p&gt;

&lt;p&gt;Given the following function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pt1filter&lt;/code&gt;. Lets assume it is everything in one file. Old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; style.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-C&quot;&gt;void PT1Filter(uint32* pOldValue, uint32 NewValue, uint16 DeltaT,
               uint16 TimeConstant) {
  uint32 DeltaV;

  if (TimeConstant &amp;gt; 0U) {
    if (TimeConstant &amp;lt; DeltaT) {
      *pOldValue = NewValue;
    } else {
      if (NewValue &amp;gt; *pOldValue) {
        DeltaV = (NewValue - *pOldValue) * (uint32)DeltaT;
        DeltaV += (uint32)TimeConstant - 1uL;
        DeltaV /= (uint32)TimeConstant;
        *pOldValue += DeltaV;
      } else {
        DeltaV = (*pOldValue - NewValue) * (uint32)DeltaT;
        DeltaV += (uint32)TimeConstant / 2uL;
        DeltaV /= (uint32)TimeConstant;

        if ((NewValue == 0uL) &amp;amp;&amp;amp; (*pOldValue != 0uL) &amp;amp;&amp;amp; (DeltaV == 0uL)) {
          DeltaV = 1;
        }

        if (*pOldValue &amp;gt; DeltaV) {
          *pOldValue -= DeltaV;
        } else {
          *pOldValue = 0;
        }
      }
    }
  } else {
    *pOldValue = NewValue;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some design considerations have been done because of the limitation of previous platforms, e.g. limited use of float or memory restrictions.&lt;/p&gt;

&lt;p&gt;Now I had somebody porting it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C++&lt;/code&gt;. Now we have the following. Well it compiles well with C++, looks a bit modern as it is now using templates, but that’s it. There has been an additional tests, for an upper and lower limit. It is not better readable nor understandable than before. And it now has &lt;strong&gt;six&lt;/strong&gt; parameters.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-CPP&quot;&gt;template &amp;lt;typename T&amp;gt;
inline T pt1Filter(const T oldValue, const T newValue, const T upperLimit,
                   const T lowerLimit, const T deltaT, const T timeConstant) {
  T deltaV;
  bool filteringEnabled{true};
  if (newValue &amp;gt;= oldValue) {
    deltaV = static_cast&amp;lt;T&amp;gt;(newValue - oldValue);
    if (deltaV &amp;gt; upperLimit) {
      filteringEnabled = false;
    }
  } else {
    deltaV = static_cast&amp;lt;T&amp;gt;(oldValue - newValue);
    if (deltaV &amp;gt; lowerLimit) {
      filteringEnabled = false;
    }
  }

  T result;
  if ((filteringEnabled == true) &amp;amp;&amp;amp; (deltaV &amp;gt; 0) &amp;amp;&amp;amp; (deltaT &amp;lt; timeConstant) &amp;amp;&amp;amp;
      (timeConstant &amp;gt; 0)) {
    const float numerator{
        static_cast&amp;lt;float&amp;gt;((oldValue * deltaT) + (newValue * timeConstant))};
    const float denominator{static_cast&amp;lt;float&amp;gt;((deltaT + timeConstant))};
    result = static_cast&amp;lt;T&amp;gt;(
        std::floor((numerator + (denominator / 2.0F)) / denominator));
  } else {
    result = newValue;
  }

  return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we choose any of the lines, we might have a clue, what’s going on here, but it is hard to get to it right away, are we?&lt;/p&gt;

&lt;p&gt;Lets make it a bit simpler.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-CPP&quot;&gt;template &amp;lt;typename T&amp;gt;
class Limits {
 public:
  T const lower;
  T const upper;

  bool isWithin(T value) const { return (value &amp;lt;= upper) &amp;amp;&amp;amp; (value &amp;gt;= lower); }
};

template &amp;lt;typename T&amp;gt;
typename std::enable_if_t&amp;lt;std::is_integral_v&amp;lt;T&amp;gt;, T&amp;gt; pt1Filter(
    T const old_value, T const new_value, Limits&amp;lt;T&amp;gt; const&amp;amp; limits,
    std::chrono::milliseconds const&amp;amp; time_delta,
    std::chrono::milliseconds const&amp;amp; time_constant) {
  auto const value_delta{new_value - old_value};

  if (!limits.isWithin(value_delta) || time_constant.count() &amp;lt;= 0 ||
      time_delta &amp;gt;= time_constant || std::abs(value_delta) == 0) {
    return new_value;
  }

  auto const numerator{static_cast&amp;lt;float&amp;gt;((old_value * time_delta.count()) +
                                          (new_value * time_constant.count()))};
  auto const denominator{
      static_cast&amp;lt;float&amp;gt;((time_delta.count() + time_constant.count()))};
  return static_cast&amp;lt;T&amp;gt;(std::lround(numerator / denominator));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So from my point of view it is a bit better. Basically I changed the following aspects:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;use standard and meaningful types,&lt;/li&gt;
  &lt;li&gt;create a type, where it made sense,&lt;/li&gt;
  &lt;li&gt;return early.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am not satisfied with this, as 5 parameters are likely to get wrong. To make it simpler. First thing is to check how it is actually called.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-CPP&quot;&gt;const uint32_t upperLimitPt1{50};
const uint32_t lowerLimitPt1{0};
....
filtered = pt1Filter(new, old, {lowerLimitPt1, upperLimitPt1}, deltaT, tconst);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What we can see here, is that the limits are set to fixed values by the caller. That should be a hint for us, to do is in another way. There is a part that changes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tconst&lt;/code&gt; but this is barely called. So maybe we can do better, and work with the limits and the time constant as kind of configuration.&lt;/p&gt;

&lt;p&gt;The limits are really a static configuration of the filter, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tconst&lt;/code&gt; is a dynamic configuration. And so I decided to put the filter into a class and take the configuration as part of the constructor:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-CPP&quot;&gt;Pt1Filter(Limits&amp;lt;T&amp;gt; const&amp;amp; limits, 
         std::chrono::milliseconds const&amp;amp; time_constant = std::chrono::milliseconds(0)) 
         : limits_{limits}, time_constant_{time_constant_} {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As we want to be able to change the time constant at some time during the runtime, we add a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setTimeConstant&lt;/code&gt; interface. The rest of the parameters stays the same and is moved into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator()&lt;/code&gt; member.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-CPP&quot;&gt;template &amp;lt;typename T, typename = std::enable_if_t&amp;lt;std::is_integral_v&amp;lt;T&amp;gt;&amp;gt;&amp;gt;
class Pt1Filter {
 public:
  Pt1Filter(Limits&amp;lt;T&amp;gt; const&amp;amp; limits,
            std::chrono::milliseconds const&amp;amp; time_constant =
                std::chrono::milliseconds(0))
      : limits_{limits}, time_constant_{time_constant_} {}
  void setTimeConstant(std::chrono::milliseconds const&amp;amp; time_constant) {
    time_constant_ = time_constant_;
  }

  T operator()(T const old_value, T const new_value,
               std::chrono::milliseconds const&amp;amp; time_delta) const {
    auto const value_delta{new_value - old_value};

    if (!limits_.isWithin(value_delta) || time_constant_.count() &amp;lt;= 0 ||
        time_delta &amp;gt;= time_constant_ || isValidDelta(value_delta)) {
      return new_value;
    }

    auto const numerator{
        static_cast&amp;lt;float&amp;gt;((old_value * time_delta.count()) +
                           (new_value * time_constant_.count()))};
    auto const denominator{
        static_cast&amp;lt;float&amp;gt;((time_delta.count() + time_constant_.count()))};
    return static_cast&amp;lt;T&amp;gt;(std::lround(numerator / denominator));
  }

 private:
  Limits&amp;lt;T&amp;gt; const&amp;amp; limits_;
  std::chrono::milliseconds time_constant_;

  inline bool isValidDelta(T delta) const { return std::abs(delta) != 0; }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now lets reduce the functions parameters.&lt;/p&gt;

&lt;p&gt;So what changes did I actually made.&lt;/p&gt;

&lt;p&gt;Used types that represent what I actually care about. In this example I changed the type of the parameter which represents a certain amount of time to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::chrono::duration&lt;/code&gt;. The algorithm makes use of some limits. Making the upper and lower limit part of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;struct&lt;/code&gt; makes the signature easier to read, understand and harder to pass the parameters in the incorrect order. And finally, I removed the parameters which are actual &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; anyway in the callers context and passed in every time. What we get is a lean interface and using standard library also a clean implementation which is (nearly) readable by non coders.&lt;/p&gt;

&lt;h2 id=&quot;tldr&quot;&gt;&lt;a href=&quot;#tldr&quot;&gt;&lt;/a&gt;TL;DR&lt;/h2&gt;

&lt;p&gt;If you ever porting legacy code consider the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;understand the intention&lt;/li&gt;
  &lt;li&gt;use proper, expressive types (use existing or create new ones)&lt;/li&gt;
  &lt;li&gt;know the std algorithms&lt;/li&gt;
  &lt;li&gt;try to make the interfaces as small as possible&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;extra&quot;&gt;&lt;a href=&quot;#extra&quot;&gt;&lt;/a&gt;Extra&lt;/h2&gt;

&lt;p&gt;How to ensure it really works? There are no tests available for the legacy implementation? You are lazy?
I did it like that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;move the old implementation to the test code&lt;/li&gt;
  &lt;li&gt;wrote a data driven testcase, with a lot of combinations, taken from the code, utilizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;testing::Combine&lt;/code&gt;. The test only compares the original result with the one of the reimplementation.&lt;/li&gt;
  &lt;li&gt;did the reimplementation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had about 3.5 million tests to verify my solution actually works. It is not the kind of test you may want to run every time. It may also break your test result reporting tool. 
But while implementing such a replacement I found it very helpful and pleasing to know it actually works.&lt;/p&gt;

</description>
        <pubDate>Thu, 29 Apr 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/04/29/Legacy-Software-Rewrite.html</link>
        <link href="https://m42e.de/2021/04/29/Legacy-Software-Rewrite.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/04/29/Legacy-Software-Rewrite.html</guid>
      </item>
    
      <item>
        <title>What are Inline Namespaces?</title>
        <description>&lt;p&gt;Inline namespaces are transparent for those who are using the outer namespace.
This is useful if you want to allow to narrow down the functions/classes/constants used, but do not want to add some more clutter if somebody wants to have them all.&lt;/p&gt;

&lt;p&gt;A simple example:&lt;/p&gt;

&lt;h2 id=&quot;simple-example&quot;&gt;&lt;a href=&quot;#example&quot;&gt;&lt;/a&gt;Simple Example&lt;/h2&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m42e&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nested&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my23&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// namespace nested&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// namespace test&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// namespace cpp&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// namespace m42e&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;c1&quot;&gt;// this works for sure&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m42e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m42e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nested&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my23&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m42e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// of course we can use my42 here&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// as nested is inline it is &quot;transparent&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my23&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sat, 24 Apr 2021 00:00:00 +0000</pubDate>
        <link>https://m42e.de//2021/04/24/Cpp-Inline-Namespaces.html</link>
        <link href="https://m42e.de/2021/04/24/Cpp-Inline-Namespaces.html"/>
        <guid isPermaLink="true">https://m42e.de/2021/04/24/Cpp-Inline-Namespaces.html</guid>
      </item>
    
  </channel>
</rss>
