<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.4">Jekyll</generator><link href="https://sdmg15.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://sdmg15.com/" rel="alternate" type="text/html" /><updated>2023-06-29T08:38:00+00:00</updated><id>https://sdmg15.com/feed.xml</id><title type="html">sdmg15</title><subtitle>Git Evangelist,C++ Alchemist, Engineering Africa</subtitle><entry><title type="html">:: Ubuntu 23 Workspaces</title><link href="https://sdmg15.com/blog/2023/06/28/u23-workspaces/" rel="alternate" type="text/html" title=":: Ubuntu 23 Workspaces" /><published>2023-06-28T00:00:00+00:00</published><updated>2023-06-28T00:00:00+00:00</updated><id>https://sdmg15.com/blog/2023/06/28/u23-workspaces</id><content type="html" xml:base="https://sdmg15.com/blog/2023/06/28/u23-workspaces/">&lt;p&gt;A new feature I discovered after installing Ubuntu 23.&lt;/p&gt;

&lt;p&gt;Tbh it’s not quite new feature, it exists since but I never took advantage nor given any time to it.
But recently I switched to a new computer and installed Ubuntu 23 on it. Coming from ubuntu 18.04.&lt;/p&gt;

&lt;p&gt;I was really impressed by the way the UI has evolved a lot. It’s kind of more smooth and more attractive than previous versions I think, again this 
is only my point of view.&lt;/p&gt;

&lt;p&gt;The new Ubuntu 23 quite looks like MacOS :) yeah I think they have some points in common.&lt;/p&gt;

&lt;p&gt;As also a Mac user I’ve always wanted to have the three fingers touch  switch on Ubuntu. For those of you who don’t know what it is here is a sample
of what using three fingers on the touchpad looks like on Mac.&lt;/p&gt;

&lt;video muted=&quot;&quot; controls=&quot;&quot; style=&quot;width: 400px; height: 500px&quot;&gt;
    &lt;source src=&quot;/assets/images/ws-macos.mp4&quot; type=&quot;video/mp4&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;With the advent of Ubuntu 23 (Lunar codename) we have something similar, not quite similar but that can be a “replacement” of that with more features: WORKSPACES.&lt;/p&gt;

&lt;p&gt;As said at the begining the feature is not quite new, it’s just that it’s now I’m starting taking advantage of it, and I thought it will be worth sharing
maybe this could help more developers in increasing their productivity.&lt;/p&gt;

&lt;h1 id=&quot;so-workspaces-what-are-they&quot;&gt;So workspaces? What are they?&lt;/h1&gt;

&lt;p&gt;With workspace you could have a great segregation between differents tasks/projects you’re working on.&lt;/p&gt;

&lt;p&gt;If you’re like me you could have 2 or more projects you could be thinkering on and sometimes you have multiple code editors opened
while doing switching maybe from your code editor to browser and back you may get inside the 
wrong editor stuffs like that. This will take you time to look again for the right code editor where the project you’re working on is opened etc.&lt;/p&gt;

&lt;p&gt;One of the tools that is used daily is browser. As developer we sometimes have ton of browser tabs for multiple projects and we can easily get lost 
in searching tabs that are related to the project we’re working on, with workspaces this belongs to the past.&lt;/p&gt;

&lt;p&gt;Workspace helps you separate different instances of applications you’re using for differents projects. 
They will really help you increasing your productivity while being organised.
specific&lt;/p&gt;

&lt;p&gt;If you like multi-tasking, you could have an external monitor, one workspace is on the monitor another one in your laptop !&lt;/p&gt;

&lt;h1 id=&quot;creating-workspaces-limited-vs-unlimited&quot;&gt;Creating workspaces: limited vs unlimited&lt;/h1&gt;

&lt;p&gt;In order to save some memory you could limit the number of workspaces you want to created during your sesssion.
To do so open &lt;code class=&quot;highlighter-rouge&quot;&gt;Settings&lt;/code&gt; then go to &lt;code class=&quot;highlighter-rouge&quot;&gt;MultiTasking&lt;/code&gt;. In the same window there are more settings related to multitasking adjust and tweak atr your convenience.
This will all depend on the amount of memory you have so choose wisely.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/ws-settings.png&quot; alt=&quot;Workspace Settings&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In order to create a new workspace, wherever you’re at this moment use three fingers and swipe up on your touchpad. Once done you’ll have window similar to the one below.&lt;/p&gt;

&lt;p&gt;As An alternative to the previous method, you could just press&lt;code class=&quot;highlighter-rouge&quot;&gt;super key&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;windows key&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/ws-view.png&quot; alt=&quot;Creating WS&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The three small windows at the top there represents the number of active workspaces I have. And the last one is where you could click to create a new workspace. And… that’s it, upon click, your new and fresh workspace is ready.&lt;/p&gt;

&lt;p&gt;Switching between workspaces is really easy, you swipe horizontally with three fingers on the direction where the workspace you’re heading to is located and voila!&lt;/p&gt;

&lt;video muted=&quot;&quot; controls=&quot;&quot; style=&quot;width: 800px; height: 800px&quot;&gt;
    &lt;source src=&quot;/assets/images/ws-cast.mp4&quot; type=&quot;video/mp4&quot; /&gt;
&lt;/video&gt;

&lt;p&gt;What’s really fascinating is how the apps on the dock on the left adapts to be only to the one that I have opened on a specific workspace.&lt;/p&gt;

&lt;h1 id=&quot;workspaces-and-memory-consumption&quot;&gt;Workspaces and memory consumption&lt;/h1&gt;

&lt;p&gt;I haven’t done any benchmarking yet between having 10 tabs opened in one browser and have two distinct instances each with 5 tabs for example.
If you have more information about this you can drop them in the comments. In the meantime I’ll conduct my own research and update this section accordingly.&lt;/p&gt;

&lt;p&gt;The only advice I can give here at the moment, is that if you notice any memory consumption spikes, just think about reducing or limiting the number of workspaces you have.&lt;/p&gt;

&lt;p&gt;This is just a quick productivity hack I thought sharing, some may have been using this since previous versions of ubuntu, but as said I knew them since but never bothered checking at them, now I did and found them interesting.&lt;/p&gt;

&lt;p&gt;What’s you’re experience working with ubuntu workspaces?
Until next time !&lt;/p&gt;</content><author><name></name></author><category term="productivity" /><category term="hacks" /><category term="Ubuntu" /><summary type="html">A new feature I discovered after installing Ubuntu 23.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://sdmg15.com/assets/images/posts/ws-view.png" /></entry><entry><title type="html">:: C++ User Defined Literals</title><link href="https://sdmg15.com/blog/2020/08/21/cpp-udl/" rel="alternate" type="text/html" title=":: C++ User Defined Literals" /><published>2020-08-21T00:00:00+00:00</published><updated>2020-08-21T00:00:00+00:00</updated><id>https://sdmg15.com/blog/2020/08/21/cpp-udl</id><content type="html" xml:base="https://sdmg15.com/blog/2020/08/21/cpp-udl/">&lt;p&gt;The advent of C++11 introduced a lot of new awesome features into the language going from &lt;strong&gt;Lambda Expressions&lt;/strong&gt;, &lt;strong&gt;Exception Specification with noexcept&lt;/strong&gt;, &lt;strong&gt;Constant Expressions&lt;/strong&gt;, &lt;strong&gt;auto type specifier&lt;/strong&gt; to &lt;strong&gt;User Defined Literal&lt;/strong&gt; and more…  I’ve already covered the basics of Lambda in my previous post, if you haven’t read it yet I recommend you to.
Today what will get our attention are &lt;strong&gt;UDLs&lt;/strong&gt;. &lt;strong&gt;UDL&lt;/strong&gt; or &lt;strong&gt;U&lt;/strong&gt;ser &lt;strong&gt;D&lt;/strong&gt;efined &lt;strong&gt;L&lt;/strong&gt;iteral for long is a C++ feature that helps you provide some shorthand construction of your object types, to the user of your interface.&lt;/p&gt;

&lt;p&gt;The standard library itself comes in with some predefined literal suffixes suchs as &lt;code class=&quot;highlighter-rouge&quot;&gt;sv&lt;/code&gt; (String View) which lets you contruct a string view from a string literal e.g &lt;code class=&quot;highlighter-rouge&quot;&gt;auto myStringView = &quot;This is a new string view&quot;sv;&lt;/code&gt; In order to be able to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;sv&lt;/code&gt; literal you’ll need to use the required namespace &lt;code class=&quot;highlighter-rouge&quot;&gt;using namespace std::literals&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You could also write custom suffixes for your custom types  e.g &lt;code class=&quot;highlighter-rouge&quot;&gt;auto myType = &quot;some string for my type&quot;_mytype;&lt;/code&gt; that’s where this post aim to drive you through.&lt;/p&gt;

&lt;p&gt;The language has a ton of predefined literals that we won’t cover here but instead will see how to write ours. But before being able to do so there are some few rules as usually that we need to be aware of. Ready? Let’s go …&lt;/p&gt;

&lt;h1 id=&quot;the-syntax-and-supported-parameter-types&quot;&gt;The syntax and supported parameter types&lt;/h1&gt;

&lt;p&gt;As during each war, if you want to have a chance to come out winner, you need to get prepared well, this is what this section is all about means preparing our weapons !&lt;/p&gt;

&lt;h3 id=&quot;the-syntax&quot;&gt;The Syntax&lt;/h3&gt;

&lt;p&gt;The syntax of UDLs is almost similar to the one you use when declaring a function or a function template operators. The defined operator is called &lt;strong&gt;Literal Operator&lt;/strong&gt; or &lt;strong&gt;Literal Operator Template&lt;/strong&gt; if it’s a template.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Yes man thanks for the info, but please dissimulate the doubt in me…&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hehe yeah let’s move straight to it. Considering that it follows the same rules as normal functions declaration we’ll omit return type specification and other specifiers to just focus on what interest us.&lt;/p&gt;

&lt;p&gt;The global syntax follows this pattern:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_ud&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ParamType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Where :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;_ud-suffix&lt;/strong&gt; is a character sequence preceded by an underscore (&lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt;) that will constitute your actual customized prefix.
&lt;strong&gt;eg&lt;/strong&gt;: &lt;code class=&quot;highlighter-rouge&quot;&gt;auto operator &quot;&quot;_myType(ParamType)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ParamType&lt;/strong&gt; is the type that will be used when the compiler will be perfoming the unqualified lookup to match the viable overloaded operator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The values that &lt;strong&gt;ParamType&lt;/strong&gt; supports are discussed in the next section.
The syntax is so simple right? Now let’s see what are the values supported by &lt;strong&gt;ParamType&lt;/strong&gt; to complete our cake.&lt;/p&gt;

&lt;h3 id=&quot;supported-parameter-types&quot;&gt;Supported parameter types&lt;/h3&gt;

&lt;p&gt;As stated earlier, UDL will let you produce objects of your defined type by providing a custom suffix. The only literal types that are allowed before your suffix are &lt;strong&gt;integer literal&lt;/strong&gt;, &lt;strong&gt;character literal&lt;/strong&gt;, &lt;strong&gt;string literal&lt;/strong&gt; and &lt;strong&gt;floating point literal&lt;/strong&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_km&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Integer Literal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monthObj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;january&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_month&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// String Literal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.49&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_kg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Floating Point Literal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asciiCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'C'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_ascii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character Literal&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For every types in the previous listing, there should be a corresponding overloaded operator defined, else the program will be ill-formed:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;For user defined integer literals&lt;/strong&gt;, the compiler will perfom an unqualified lookup and if the overload set includes a definition with &lt;strong&gt;ParamType&lt;/strong&gt; equals to &lt;code class=&quot;highlighter-rouge&quot;&gt;unsigned long long&lt;/code&gt; this overload will be selected and the user defined literal expression will be considered as a call to the function &lt;code class=&quot;highlighter-rouge&quot;&gt;operator &quot;&quot; myType(xULL)&lt;/code&gt;
where  &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; is the literal without the &lt;code class=&quot;highlighter-rouge&quot;&gt;ud-suffix&lt;/code&gt;. &lt;strong&gt;eg:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s suppose we are in a stock market and we want to place some orders to buy 100 shares of an asset:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;type_traits&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BUY&lt;/span&gt; &lt;span class=&quot;o&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;n&quot;&gt;SELL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;explicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Order&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;n&quot;&gt;qty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;side&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BUY&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;n&quot;&gt;qty_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;side_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;side&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;n&quot;&gt;qty_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;side_&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;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qty&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;n&quot;&gt;Order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qty&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;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;newBuyOrder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_order&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;newBuyOrder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qty_&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;&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/4hxvxc&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can interpret the line 20 as &lt;code class=&quot;highlighter-rouge&quot;&gt;auto newBuyOrder = operator &quot;&quot;_order(100ULL)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;For user defined floating point literals&lt;/strong&gt;, the compiler will still perfom the unqualified lookup and if the overload set includes a definition with &lt;strong&gt;ParamType&lt;/strong&gt; equals to &lt;code class=&quot;highlighter-rouge&quot;&gt;long double&lt;/code&gt; this overload will be selected and the user defined literal expression will be considered as a call to the function &lt;code class=&quot;highlighter-rouge&quot;&gt;operator &quot;&quot; myType(fL) &lt;/code&gt; where  &lt;code class=&quot;highlighter-rouge&quot;&gt;f&lt;/code&gt; is the literal without the &lt;code class=&quot;highlighter-rouge&quot;&gt;ud-suffix&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_kg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&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;// Construct some object here&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;main&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;kt&quot;&gt;int&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;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.10&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_kg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can interpret the line 5 as &lt;code class=&quot;highlighter-rouge&quot;&gt;auto weight = operator &quot;&quot;_kg(10.10L)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;For user defined character literals&lt;/strong&gt; (all its variant included: &lt;code class=&quot;highlighter-rouge&quot;&gt;wchar_t&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;char8_t&lt;/code&gt; etc.) a corresponding overload with the variant type need to be defined, except the case where some can fit into others.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&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;n&quot;&gt;Person&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;n&quot;&gt;c&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;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'C'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// will call operator &quot;&quot; p(C)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;u8'P'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// compiles, p2 type is char until C++20&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P3&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Will fail, need to include wchar_t in the overload set&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;For user defined string literals&lt;/strong&gt; (all its variants included, &lt;code class=&quot;highlighter-rouge&quot;&gt;const wchar_t*&lt;/code&gt;,  &lt;code class=&quot;highlighter-rouge&quot;&gt;const char16_t&lt;/code&gt; etc) an overload following the form&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_ud&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&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;kt&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;need to be defined. Where &lt;code class=&quot;highlighter-rouge&quot;&gt;Type&lt;/code&gt; is one of the variant and &lt;code class=&quot;highlighter-rouge&quot;&gt;std::size_t&lt;/code&gt; is the size of the string excluding the terminating null character &lt;code class=&quot;highlighter-rouge&quot;&gt;\0&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt; 
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Person&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;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&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;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&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;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&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;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&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;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&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;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&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;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;king arthur&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/654jjE&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: For &lt;strong&gt;user defined integer literals&lt;/strong&gt; and &lt;strong&gt;floating point literals&lt;/strong&gt;, if the overload set doesn’t include what is described above, but there is an overload for &lt;strong&gt;character literals&lt;/strong&gt;, that overload will be used as fallback by the compiler otherwise the program will be ill-formed. Let’s see that with an example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Money&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;explicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Money&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;n&quot;&gt;amount&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;n&quot;&gt;amount_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amount&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;n&quot;&gt;amount_&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;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_euro&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;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&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;// The amount is a string, cast it to int&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// then construct the object&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Money&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;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amout&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;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// call operator &quot;&quot; euro(&quot;100&quot;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;money&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_euro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This line 14 will then correspond to a call to
&lt;code class=&quot;highlighter-rouge&quot;&gt;operator &quot;&quot;_euro(&quot;100&quot;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You said literal operator template are similar to function template declaration, but I wrote a User Defined Literal operator template and the compiler is yelling at me :sad:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Oh don’t do that, at least without following the rules. The rules for defining literal operator template are as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;The template parameter should be only one argument which must be a non-type template parameter pack with type &lt;code class=&quot;highlighter-rouge&quot;&gt;char&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The operator should have empty parameter list, means the function should not take any argument&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In terms of code it means the declaration should be of this form &lt;code class=&quot;highlighter-rouge&quot;&gt;template&amp;lt;char...&amp;gt; auto operator &quot;&quot;_P()&lt;/code&gt;. The call to such operator will result in &lt;code class=&quot;highlighter-rouge&quot;&gt;operator &quot;&quot;_P&amp;lt;'c1', 'c2', 'c3'..., 'ck'&amp;gt;()&lt;/code&gt;, where c1..ck are the individual characters of the integer or floating point literal.&lt;/p&gt;

&lt;p&gt;Let’s suppose we want to sum all digits of a given number we could write something like this&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;    &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;    &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;array&amp;gt;
&lt;/span&gt;    &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;algorithm&amp;gt;
&lt;/span&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;kt&quot;&gt;char&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;o&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_sumOfDigits&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;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;span class=&quot;o&quot;&gt;&amp;amp;&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&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;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&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;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;for_each&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;begin&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;end&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;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;sc&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;span class=&quot;p&quot;&gt;}({&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&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;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;912&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_sumOfDigits&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;The sum of digits is : &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;sum&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;&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The UDL at line 17 will be treated as a function call &lt;code class=&quot;highlighter-rouge&quot;&gt;operator &quot;&quot;_sumOfDigits&amp;lt;'9, '1', '2'&amp;gt;()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The defined literal fo string_view (sv) you shown doesn’t have an underscrore at the beginning why does my own has one, are you kidding me ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Oh good catch! The literals that don’t have the underscore before their suffix are &lt;strong&gt;reserved&lt;/strong&gt; means only the standard library can define such literals alas!&lt;/p&gt;

&lt;h1 id=&quot;users-and-use-cases-of-udls&quot;&gt;Users and use cases of UDLs&lt;/h1&gt;

&lt;p&gt;You’ve scrolled enough ! If you reached here means you already know the basics of UDL congrats. But there may be still this question intriguing you: who uses UDLs and when should I use them?&lt;/p&gt;

&lt;p&gt;Well, there are a lot of libraries out there that provide UDL for their types. For example &lt;strong&gt;nlohmann::json&lt;/strong&gt;  which is a JSON library for modern C++ provides the suffix &lt;code class=&quot;highlighter-rouge&quot;&gt;_json&lt;/code&gt; so that you can construct JSON from raw string literals.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;R&quot;({&quot;OrdID&quot;: &quot;1&quot;, &quot;Instrument&quot;: &quot;btc_usd&quot;})&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;strong&gt;std::chrono&lt;/strong&gt;  library has also a lot of defined literals to help you construct time and date easily and quickly.
Remember to include the required namespace with &lt;code class=&quot;highlighter-rouge&quot;&gt;using namespace std::literals&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;milliseconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ms&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;seconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&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;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;UDL comes in a handy way when you’re writting your unit tests as stated in this talk by Matt Godbolt (&lt;a href=&quot;https://youtu.be/nLSm3Haxz0I?t=1149&quot;&gt;https://youtu.be/nLSm3Haxz0I?t=1149&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I also wrote a toy library called &lt;strong&gt;Nkap&lt;/strong&gt; to demonstrate the usage of UDLs, you can check the source code of the repo &lt;a href=&quot;https://github.com/sdmg15/nkap&quot;&gt;https://github.com/sdmg15/nkap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is just to name few, you can also go ahead and take advantage of the feature to provide such shorthand objects construction for your types !&lt;/p&gt;

&lt;h1 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h1&gt;

&lt;p&gt;In the stampede to modern C++, adding UDL to your toolbox will help you write strong programs, keeping you also at the cutting edge. I admit it’s a lot to take in, but recognize with me that the pain is temporary whilst the glorry is eternal :smile: !&lt;/p&gt;

&lt;h1 id=&quot;references&quot;&gt;References&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/language/user_literal&quot;&gt;https://en.cppreference.com/w/cpp/language/user_literal&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/language/integer_literal&quot;&gt;https://en.cppreference.com/w/cpp/language/integer_literal&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://eel.is/c++draft/expr.prim.literal&quot;&gt;https://eel.is/c++draft/expr.prim.literal&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://eel.is/c++draft/lex.ext#nt:user-defined-literal&quot;&gt;https://eel.is/c++draft/lex.ext#nt:user-defined-literal&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><category term="C++" /><category term="UDL" /><category term="Literal" /><summary type="html">The advent of C++11 introduced a lot of new awesome features into the language going from Lambda Expressions, Exception Specification with noexcept, Constant Expressions, auto type specifier to User Defined Literal and more… I’ve already covered the basics of Lambda in my previous post, if you haven’t read it yet I recommend you to. Today what will get our attention are UDLs. UDL or User Defined Literal for long is a C++ feature that helps you provide some shorthand construction of your object types, to the user of your interface.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://sdmg15.com/assets/images/posts/cover.png" /></entry><entry><title type="html">:: C++ lambda: The basics</title><link href="https://sdmg15.com/blog/2019/11/24/cpp-lambda-basics/" rel="alternate" type="text/html" title=":: C++ lambda: The basics" /><published>2019-11-24T00:00:00+00:00</published><updated>2019-11-24T00:00:00+00:00</updated><id>https://sdmg15.com/blog/2019/11/24/cpp-lambda-basics</id><content type="html" xml:base="https://sdmg15.com/blog/2019/11/24/cpp-lambda-basics/">&lt;p&gt;If you have a Math background, you’ll have probably already heard about Lambda. In Programming in general and in C++ specifically, the concept may not resemble at all at what you think but don’t be afraid, in this post I’ll briefly introduce you to what Lambdas are and how to use and take advantage from them.&lt;/p&gt;

&lt;h1 id=&quot;before-lambda-the-earlier-age-&quot;&gt;Before lambda: the earlier age …&lt;/h1&gt;

&lt;p&gt;The Standard Template Library (STL) is composed of containers. Iterating over them is quite easy, you can choose to use
a build-in Algorithm like &lt;code class=&quot;highlighter-rouge&quot;&gt;for_each&lt;/code&gt; or even use a raw &lt;code class=&quot;highlighter-rouge&quot;&gt;for&lt;/code&gt; loop. But as a rule of thumb it’s recommended to use standard algorithms, because they express well the intend and make the code cleaner.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;for_each&lt;/code&gt; algorithm overload that we will use takes three parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;The first one is an iterator pointing at the beginning of the container&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The second one is another iterator but that points at this time after the end of the container&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The third one is a Callable&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Callable maybe a class or a struct that overrides the bracket operator (&lt;code class=&quot;highlighter-rouge&quot;&gt;operator()&lt;/code&gt;), or a function pointer/reference.&lt;/p&gt;

&lt;p&gt;Let’s enlight our words with the following example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt; 
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCallable&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;k&quot;&gt;operator&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;n&quot;&gt;param&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;param&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;&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;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When passed as the third argument of &lt;code class=&quot;highlighter-rouge&quot;&gt;for_each&lt;/code&gt; the operator will be called with the specified argument.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt; 
&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;array&amp;gt;
#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCallable&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;k&quot;&gt;operator&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;n&quot;&gt;param&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;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;param&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;&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;p&quot;&gt;}&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;main&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;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;array&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;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;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;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;2&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;mi&quot;&gt;4&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;for_each&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;cbegin&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;cend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCallable&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;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/qyCRm2&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above snippet will print the elements of the container. This is what we call High Order Functions (HOF).&lt;/p&gt;

&lt;p&gt;As you can see we were obliged to declare an external class and then overrides its bracket operator before being able to use it as a callback in the &lt;code class=&quot;highlighter-rouge&quot;&gt;for_each&lt;/code&gt; algorithm. It maybe annoying having to do that everytime that we need a callback for doing a specific task.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wait, you’re saying that everytime that I need a callback I’ll have to write such class? I’m abandoning here …&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;No don’t run out, I’m sure you have guessed that if I’m talking to you about that “problem” it’s because a solution actually exists, just keep scrolling :wink:.&lt;/p&gt;

&lt;h1 id=&quot;lambda-to-the-rescue&quot;&gt;Lambda to the rescue&lt;/h1&gt;

&lt;p&gt;Lambda is a feature introduce since C++11. It provides a concise way to create simple function object.
The code we wrote earlier can be written like this using lambdas :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;// Compiler Explorer : https://godbolt.org/z/Fr4utK&lt;/span&gt;

  &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;array&amp;gt;
&lt;/span&gt;  &lt;span class=&quot;cp&quot;&gt;#include&amp;lt;iostream&amp;gt;
&lt;/span&gt;  &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;algorithm&amp;gt;
&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;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;array&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;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;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;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;2&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;mi&quot;&gt;4&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;for_each&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;cbegin&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;cend&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;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param&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;param&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;&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;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/Fr4utK&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we are not anymore using &lt;code class=&quot;highlighter-rouge&quot;&gt;TestCallable&lt;/code&gt; as the third parameter.
The two code will basically perform the same task, just that this one is using a lambda. Cleaner right?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dude is that obscure syntax you want me to introduce in my code base?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, the lambda syntax can be obscure but once you’ve understood the meaning of each branches you will feel that your soul
is now different from others :smile:&lt;/p&gt;

&lt;h1 id=&quot;depicting-the-different-parts-of-a-lambda&quot;&gt;Depicting the different parts of a Lambda&lt;/h1&gt;

&lt;h4 id=&quot;overall-structure&quot;&gt;Overall structure&lt;/h4&gt;

&lt;p&gt;A lambda is a closure type. It’s structured as follow since C++11:
&lt;code class=&quot;highlighter-rouge&quot;&gt;[](parameters) *specifier* -&amp;gt; *trailing return type* { *body* }&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[]&lt;/code&gt;: called the &lt;strong&gt;capture list&lt;/strong&gt; or &lt;strong&gt;lambda introducer&lt;/strong&gt;. It’s there that we will specify the variables that will be available in the Lambda scope. It helps introducing the lambda.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;(parameters)&lt;/code&gt;: Called the &lt;strong&gt;parameter list&lt;/strong&gt;. It’s inside the brackets that we will specify the parameters that the lambda will take when called. You probably already know this from traditionnal functions. The brackets are optional.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;specifier&lt;/code&gt;: It can be either &lt;code class=&quot;highlighter-rouge&quot;&gt;constexpr&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;mutable&lt;/code&gt;, it’s an optional value.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Trailing return type&lt;/code&gt;: It’s an optoinal value, preceeded by an arrow &lt;code class=&quot;highlighter-rouge&quot;&gt;-&amp;gt;&lt;/code&gt; which specifies that we are using the trailing return type syntax. When not specified the return type of the lambda will be deduced using &lt;strong&gt;template type deduction&lt;/strong&gt; rules.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;body&lt;/code&gt;: It’s where you write the content of your lambda.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all that on hands, we can now write as much examples as we can:&lt;/p&gt;

&lt;p&gt;Example One:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;// Compiler Explorer https://godbolt.org/z/ysp-p_&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;sumLambda&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;int&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;kt&quot;&gt;int&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;o&quot;&gt;-&amp;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;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;span class=&quot;c1&quot;&gt;// Declaring about lambda&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sumLambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Calling the lambda with 4 and 5 as parameters &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;The sum is: &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;sum&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;&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;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/ysp-p_&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example Two:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&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;changeAge&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;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&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;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;age&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;&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;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;changeAge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Invoking the lambda at this point taking not parameters.&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/HVX5er&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When writting such syntax, internally the compiler will generate a unique unnamed class that overrides the &lt;code class=&quot;highlighter-rouge&quot;&gt;operator()&lt;/code&gt; just like we did before. The type of the lambda is only known at compile time, we need to use &lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt; keyword so that the compiler will replace it by a type he generated.&lt;/p&gt;

&lt;p&gt;The generated &lt;code class=&quot;highlighter-rouge&quot;&gt;operator()&lt;/code&gt; by the compiler is by default &lt;code class=&quot;highlighter-rouge&quot;&gt;const&lt;/code&gt; if we didn’t explicitly required it not to be by putting &lt;code class=&quot;highlighter-rouge&quot;&gt;mutable&lt;/code&gt; in the &lt;code class=&quot;highlighter-rouge&quot;&gt;specifier&lt;/code&gt; list.&lt;/p&gt;

&lt;p&gt;To visualize how the compiler generates that I used &lt;a href=&quot;https://cppinsights.io/&quot;&gt;CppInsights&lt;/a&gt; which helps us see what the compiler sees :eyes:. Here is the link to the &lt;strong&gt;First Example&lt;/strong&gt; using cppinsights, https://cppinsights.io/s/3feb5a61. There you’ll see and understand more about what I was describing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Okay cook but dude, your second example is not running what’s going on?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Oh well, even me I don’t know what’s wrong. Just kidding let’s move to the next section to understand what’s wrong.&lt;/p&gt;

&lt;h4 id=&quot;lambda-captures-list&quot;&gt;Lambda captures list&lt;/h4&gt;

&lt;p&gt;In the lambda body, variables of the parent scope (the scope in which the lambda was declared) are not available. If you want to make them being available you’ll need to specify that in the &lt;strong&gt;capture list&lt;/strong&gt;. We don’t specify them randomly, there are some rules to follow:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[=]&lt;/code&gt;: will capture all variables in the parent scope by copy.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[&amp;amp;]&lt;/code&gt;: will capture all variables in the parent scope by reference.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[&amp;amp;a,b]&lt;/code&gt;: will capture &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; by reference and &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt; by value&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[&amp;amp;a]&lt;/code&gt;: will capture &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; from the parent scope by reference. It’ll be the only variable captured in this case.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[a]&lt;/code&gt;: will capture &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; from the parent scope by copy. It’ll be the only variable captured in this case.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[&amp;amp;a,=]&lt;/code&gt;: will capture &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; by reference and others by copy.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should note that any capture may appear only once, so you cannot &lt;code class=&quot;highlighter-rouge&quot;&gt;[a,a]&lt;/code&gt; else the compiler will scream of pain. You also cannot do something like &lt;code class=&quot;highlighter-rouge&quot;&gt;[&amp;amp;, &amp;amp;i](){};&lt;/code&gt; because you are already capturing all by reference, so the second term is not necessary. Those are just few examples of misuses of the capture list that will leave your compiler unhappy.&lt;/p&gt;

&lt;p&gt;So coming back to our Example 2 we can rewrite it accordinly:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Defining the lambda and capturing `age` by reference&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;changeAge&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;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](){&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&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;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;age&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;&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;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;changeAge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Invoking the lambda at this point taking not parameters.&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/fKVzJj&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you were to capture &lt;code class=&quot;highlighter-rouge&quot;&gt;age&lt;/code&gt; by value, you’ll have a compilation error, untill you write &lt;code class=&quot;highlighter-rouge&quot;&gt;mutable&lt;/code&gt; in the &lt;code class=&quot;highlighter-rouge&quot;&gt;specifier&lt;/code&gt;part of the lambda, remember, this is because the generated &lt;code class=&quot;highlighter-rouge&quot;&gt;operator()&lt;/code&gt; is by default &lt;code class=&quot;highlighter-rouge&quot;&gt;const&lt;/code&gt; and in the lambda body we are modifying the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;age&lt;/code&gt;. The code will look like this if you want to capture &lt;code class=&quot;highlighter-rouge&quot;&gt;age&lt;/code&gt; by value:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Defining the lambda and capturing `age` by value&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;changeAge&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;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mutable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&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;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;age&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;&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;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;changeAge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Invoking the lambda at this point taking no parameters.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/afW2BC&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if we were only reading the variable &lt;code class=&quot;highlighter-rouge&quot;&gt;age&lt;/code&gt; without modifying it, the &lt;code class=&quot;highlighter-rouge&quot;&gt;mutable&lt;/code&gt; here will be useless.&lt;/p&gt;

&lt;p&gt;Note that when capturing by value, a copy of captured symbols are created in the lambda scope, so any modification of those symbols will be on their copy. If you want the modification to affect the original symbol you’ll probably need to capture by reference instead.&lt;/p&gt;

&lt;h4 id=&quot;default-capture-list-with-initializers&quot;&gt;Default Capture list with initializers&lt;/h4&gt;

&lt;p&gt;When capturing variable you may decide to give them some default values. An example will be more illustrative:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;utility&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;n&quot;&gt;fuel&lt;/span&gt; &lt;span class=&quot;o&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&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;changeFuelAndPrint&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;n&quot;&gt;fuel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&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;ltime&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;as_const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&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;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;Fuel default value in lambda: &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;fuel&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;&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;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;Read only ltime: &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;ltime&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;&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;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;changeFuelAndPrint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Will print 20&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/YcPXXY&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The variable &lt;code class=&quot;highlighter-rouge&quot;&gt;fuel&lt;/code&gt; in the lambda scope will be initialized to &lt;code class=&quot;highlighter-rouge&quot;&gt;20&lt;/code&gt;. The variable &lt;code class=&quot;highlighter-rouge&quot;&gt;ltime&lt;/code&gt; is captured by reference and initialized with a &lt;code class=&quot;highlighter-rouge&quot;&gt;time&lt;/code&gt;, with the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::as_const&lt;/code&gt; utility, the variable &lt;code class=&quot;highlighter-rouge&quot;&gt;ltime&lt;/code&gt; will be read-only in the lambda body. You can play with that by creating multiple examples.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I noticed that you declare lambda first after then calling it in another line, ca we do better?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes sure IIFE ( Immediately Invoked Function Expression)&lt;/p&gt;

&lt;h4 id=&quot;iife-and-lambda&quot;&gt;IIFE and Lambda&lt;/h4&gt;

&lt;p&gt;Lambda expressions are closure objects. They can be immediately invoked by putting &lt;code class=&quot;highlighter-rouge&quot;&gt;()&lt;/code&gt; right after their declaration.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&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;o&quot;&gt;-&amp;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;p&quot;&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;Hello IIFE&quot;&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;&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;p&quot;&gt;}();&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Invoking the lambda immediately, so it'll print &quot;Hello IIFE&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/8S92km&quot;&gt;Compiler Explorer&lt;/a&gt; 
&lt;a href=&quot;https://cppinsights.io/s/7b83c19a&quot;&gt;CppInsights Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your Lambda was taking parameters, you’ll need to specify them in the brackets.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&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;o&quot;&gt;-&amp;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;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valOne&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;n&quot;&gt;valTwo&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;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valOne&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valTwo&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;r&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;&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;p&quot;&gt;}(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Invoking the lambda immediately with parameters 4 and 2, it'll&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// print 6&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/3AwR2B&quot;&gt;Compiler Explorer&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;the-simplest-lambda-you-can-write&quot;&gt;The simplest lambda you can write&lt;/h4&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&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;kt&quot;&gt;int&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;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code above actually compiles, I won’t argue about it you have all the necessary weapons to understand how it’s behaving :wink:&lt;/p&gt;

&lt;h1 id=&quot;where-to-use-lambda&quot;&gt;Where to use Lambda&lt;/h1&gt;

&lt;p&gt;There are really a lot of places where you can take advantage of lambdas:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;You can use them as callbacks&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;They help in complex initialization scenarios&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;You can use them to perform some inner actions without actually interferring with variables in the parent scope&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;They can be used to implement custom deleters for smart pointers. Employing the lambda way without capture incur no size penalty on the resulting smart pointer.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lambda expressions come in a really handy way, but you just have not to abuse of them, still consider using traditionnal functions when necessary. If you don’t use them well you’ll fall under missed optimization opportunities.&lt;/p&gt;

&lt;h1 id=&quot;final-thoughts&quot;&gt;Final thoughts&lt;/h1&gt;

&lt;p&gt;Ah, Lambda ! We came to our end !&lt;/p&gt;

&lt;p&gt;Personnally I like the syntax of lambda syntax, it becomes intuitive after you understand how it works.
As the C++ language evolve quickly, some additions were made to lambda such as &lt;strong&gt;templated lambda (C++20)&lt;/strong&gt; it’s uncovered in this post.&lt;/p&gt;

&lt;p&gt;Now that you have enough information about this mindblowing subject, I can now throw you in the nature to go and seek deep information on it :wave:&lt;/p&gt;

&lt;h1 id=&quot;references&quot;&gt;References&lt;/h1&gt;

&lt;p&gt;1 - https://eel.is/c++draft/expr.prim.lambda&lt;/p&gt;

&lt;p&gt;2 - https://en.cppreference.com/w/cpp/language/lambda&lt;/p&gt;

&lt;p&gt;3 - https://www.bfilipek.com/2019/02/lambdas-story-part1.html&lt;/p&gt;</content><author><name></name></author><category term="cpp" /><category term="lambda" /><category term="basics" /><summary type="html">If you have a Math background, you’ll have probably already heard about Lambda. In Programming in general and in C++ specifically, the concept may not resemble at all at what you think but don’t be afraid, in this post I’ll briefly introduce you to what Lambdas are and how to use and take advantage from them.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://sdmg15.com/assets/images/posts/lambda.png" /></entry><entry><title type="html">:: GPG-signing your Git commits</title><link href="https://sdmg15.com/blog/2019/03/31/gpg-signing-your-git-commits/" rel="alternate" type="text/html" title=":: GPG-signing your Git commits" /><published>2019-03-31T00:00:00+00:00</published><updated>2019-03-31T00:00:00+00:00</updated><id>https://sdmg15.com/blog/2019/03/31/gpg-signing-your-git-commits</id><content type="html" xml:base="https://sdmg15.com/blog/2019/03/31/gpg-signing-your-git-commits/">&lt;p&gt;Have you ever wondered what the “verified” button near each commit hash in github/gitlab means? And maybe you want to have the same each time you commit?
If you are curious like me and want to have it then here we go ! In this post I will guide you along the way on how to GPG-sign your commits using Git and Github (Not mandatory). Already excited ? Let’s go!&lt;/p&gt;

&lt;h1 id=&quot;why-do-i-need-to-sign-my-commits-any-way&quot;&gt;Why do I need to sign my commits any way?&lt;/h1&gt;

&lt;p&gt;If you are the Developer of an Open Source software (Or not) that deals with people money or at least with critical information then you may need to sign your commits to prove that it was really you who did the changes and not someone else. In that way if someone takes the control of your source code and make commits without them being signed with your GPG key then people will consider them as suspicious commits. 
In one phrase signing your commits help other people to know that the changes that was made are coming from a trusted source (You or the Organization that is maintaining the code).&lt;/p&gt;

&lt;h1 id=&quot;gpg-cryptography-&quot;&gt;GPG Cryptography ?&lt;/h1&gt;

&lt;p&gt;Heuh… Has someone said cryptography?&lt;/p&gt;

&lt;p&gt;GPG stands for &lt;strong&gt;G&lt;/strong&gt;NU &lt;strong&gt;P&lt;/strong&gt;rivacy &lt;strong&gt;G&lt;/strong&gt;uard it allows you to encrypt and sign your data and communications. It uses a set of cryptographic algorithms in order to achieve those operations. With GPG utililties we will be intalling below you generate a key pair : &lt;strong&gt;Private Key&lt;/strong&gt; and &lt;strong&gt;Public key&lt;/strong&gt;. The Private key is the one required for signing commits (So it is like your password you need to keep it secret). The Public key is one you could be sharing with everyone.&lt;/p&gt;

&lt;p&gt;Don’t panic, you don’t need to master all of those algorithms or concepts in order to have your commits marked with “verified” :smile:. But if you want to know more about to read more about cryptography check &lt;a href=&quot;https://www.wikiwand.com/en/Cryptography&quot;&gt;this&lt;/a&gt; and &lt;a href=&quot;https://www.wikiwand.com/en/GNU_Privacy_Guard&quot;&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Okay thanks for those links but I want to get my commits signed :smirk:&lt;/p&gt;

&lt;h1 id=&quot;generating-a-new-gpg-key&quot;&gt;Generating a new GPG key&lt;/h1&gt;

&lt;p&gt;The operation is relatively straightforward.
We will be using &lt;a href=&quot;https://github.com&quot;&gt;Github&lt;/a&gt; as the hosting platform for our source codes.
Let’s no waste time anymore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB :&lt;/strong&gt; All the commands you’ll see below have been tested on &lt;strong&gt;Ubuntu&lt;/strong&gt; only.&lt;/p&gt;

&lt;p&gt;If you don’t yet have any existing GPG key then continue ready, else jump to the step 4.&lt;/p&gt;

&lt;p&gt;Github supports the following GPG key algorithms :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;RSA (&lt;strong&gt;R&lt;/strong&gt;ivest &lt;strong&gt;S&lt;/strong&gt;hamir &lt;strong&gt;A&lt;/strong&gt;dleman)&lt;/li&gt;
  &lt;li&gt;ElGamal&lt;/li&gt;
  &lt;li&gt;DSA (&lt;strong&gt;D&lt;/strong&gt;igital &lt;strong&gt;S&lt;/strong&gt;ignature &lt;strong&gt;A&lt;/strong&gt;lgorithm)&lt;/li&gt;
  &lt;li&gt;ECDH (&lt;strong&gt;E&lt;/strong&gt;liptic &lt;strong&gt;C&lt;/strong&gt;urve &lt;strong&gt;D&lt;/strong&gt;iffie &lt;strong&gt;H&lt;/strong&gt;ellman)&lt;/li&gt;
  &lt;li&gt;ECDSA (&lt;strong&gt;E&lt;/strong&gt;liptic &lt;strong&gt;C&lt;/strong&gt;urve &lt;strong&gt;D&lt;/strong&gt;igital &lt;strong&gt;S&lt;/strong&gt;ignature &lt;strong&gt;A&lt;/strong&gt;lgorithm)&lt;/li&gt;
  &lt;li&gt;EdDSA (&lt;strong&gt;E&lt;/strong&gt;dwards-curve &lt;strong&gt;D&lt;/strong&gt;igital &lt;strong&gt;S&lt;/strong&gt;ignature &lt;strong&gt;A&lt;/strong&gt;lgorithm)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you generate your key with an unsupported algorithm by Github you may get errors (Something I’m sure you don’t want). A way not to have any error is to continue following my steps …&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; We will need first to install the GPG command line tools. 
To do so open your favorite terminal (With &lt;code class=&quot;highlighter-rouge&quot;&gt;Ctrl + Alt + T&lt;/code&gt; in my case) and run the commands below :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;    &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
    &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gpg&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And to check if everything is installed you can run the command &lt;code class=&quot;highlighter-rouge&quot;&gt;gpg --version&lt;/code&gt;. The minimun required version for this purpose is &lt;code class=&quot;highlighter-rouge&quot;&gt;2.1.17&lt;/code&gt;.
 If everything is Ok then let us move to the step 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; To get the utility from sources or for your platform click &lt;a href=&quot;https://www.gnupg.org/download/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; To generate a new key pair paste the text below in your terminal 
 &lt;code class=&quot;highlighter-rouge&quot;&gt;gpg --full-generate-key&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;gpg --full-gen-key&lt;/code&gt; for short version.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;You will be prompted to chose the type of key you want. We could be spotting the &lt;em&gt;RSA&lt;/em&gt; which is fortunately supported by Github and is even the default algorithm. We will chose it. Press enter and continue;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;In the next prompt enter &lt;code class=&quot;highlighter-rouge&quot;&gt;4096&lt;/code&gt;, which represents the length in bits of the key;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;At this moment you will be prompted to specify the validity duration of the key. Enter &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; if you do not want the key to expire (Let us chose this option);&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the confirmation of the validity duration you will be asked some personnal information that will be linked to the key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; Make sure the email address you will enter is a &lt;strong&gt;verified email&lt;/strong&gt; address on Github.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;After entering and verifying your information you will be asked to enter a &lt;strong&gt;passphrase&lt;/strong&gt;. The passphrase here is the password that will be required to enter before using the private key. It’s a guard for preventing someone else using your keys. Enter a strong one and save it jealously!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can now take a cup of coffee while waiting the generation of the keys to be ended :coffee:&lt;/p&gt;

&lt;p&gt;Mine has finished generating. You’ll have a similar output to this one&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  gpg: key F9BE7A92520A3D02 marked as ultimately trusted
  gpg: revocation certificate stored as '/home/user/.gnupg/openpgp-revocs.d/1C4ACE74C12E904FBAFA79EDF9BE7A92520A3D02.rev'
  public and secret key created and signed.

  pub   rsa4096 2019-04-01 [SC]
        1C4ACE74C12E904FBAFA79EDF9BE7A92520A3D02
  uid                      john &amp;lt;john@mail.com&amp;gt;
  sub   rsa4096 2019-04-01 [E]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;That means your keys have been generated successfully :tada:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It’s already taking me too long to have my commits signed I’ll give up&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hey do not give up, there are only few steps left!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; The command below just displayed us public key (Notice the &lt;em&gt;pub&lt;/em&gt;), but private key is required for signing commits or tags. Enter the following command to display the private key of all your available GPG keys : &lt;code class=&quot;highlighter-rouge&quot;&gt;gpg --list-secret-keys --keyid-format LONG&lt;/code&gt;. It will now output you something like this one :&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec   rsa4096/F9BE7B91520A3D02 2019-04-01 [SC]
      1C4ACE74C12E904FBAFA79EDF9BE7A92520A3D02
uid                 [ultimate] me &amp;lt;me@mail.com&amp;gt;
ssb   rsa4096/3CA2083CEAA89B9E 2019-04-01 [E]

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

&lt;p&gt;Make sure that before running the command there is no one around you :eyes: But even if someone has your private key and don’t know your passphrase it will be impossible for the person to use your keys that is why it is always recommended to have a passphrase set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; From the previous output you need to copy the &lt;strong&gt;Key id&lt;/strong&gt; of the key you want to use. For our example the key id is &lt;code class=&quot;highlighter-rouge&quot;&gt;F9BE7B91520A3D02&lt;/code&gt; and use it with this command&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;  gpg &lt;span class=&quot;nt&quot;&gt;--armor&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--export&lt;/span&gt; F9BE7B91520A3D02&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It will print you a large set of random characters starting with &lt;code class=&quot;highlighter-rouge&quot;&gt;-----BEGIN PGP PUBLIC KEY BLOCK-----&lt;/code&gt; and ending with &lt;code class=&quot;highlighter-rouge&quot;&gt;-----END PGP PUBLIC KEY BLOCK-----&lt;/code&gt; which represents your public key. We will now add it to our github account.&lt;/p&gt;

&lt;h1 id=&quot;telling-github-about-my-generated-keys&quot;&gt;Telling Github about my Generated keys&lt;/h1&gt;

&lt;p&gt;After have successfully generated the keys we now to inform Github about the generated keys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; In the upper-right corner of any page, click your profile photo, then click Settings. As illustrated below&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/profileGithub.png&quot; alt=&quot;Settings&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; In the user settings sidebar, click SSH and GPG keys.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/settings-sidebar-ssh-keys.png&quot; alt=&quot;sidebar-ssh-keys&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Click New GPG key.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/gpg-add-gpg-key.png&quot; alt=&quot;gpg-add-gpg-key&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; In the “Key” field, paste the GPG key you copied when you generated your GPG key.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/gpg-key-paste.png&quot; alt=&quot;gpg-key-paste&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; Now Click &lt;strong&gt;Add GPG key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/gpg-add-key.png&quot; alt=&quot;gpg-add-key&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hey dude I don’t use Github what should I do?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are using Gitlab the process is quite similar to the one of Github. But if you are using none of them, then you’ll need to read the documentatiion from your favorite host to know how to do so.&lt;/p&gt;

&lt;h1 id=&quot;telling-git-about-your-signing-key&quot;&gt;Telling GIT about your signing key&lt;/h1&gt;

&lt;p&gt;At this point we are almost done! There is only one command to run in order to inform Git that we now want to use signing key for each of our commits :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;git config &lt;span class=&quot;nt&quot;&gt;--global&lt;/span&gt; user.signingkey YOUR_KEY_ID_HERE&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Run the command we used to list the Key ids before and select the one you want to use then replace the YOUR_KEY_ID_HERE with it. So in our case we will have &lt;code class=&quot;highlighter-rouge&quot;&gt;git config --global user.signingkey F9BE7B91520A3D02&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I just did a new commit with &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit -m &quot;Testing&quot;&lt;/code&gt; but it didn’t work what’s going on ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ha I can realize that you are really hurried :sweat_smile:&lt;/p&gt;

&lt;p&gt;For the commit being definitely signed, you need to add another option (&lt;code class=&quot;highlighter-rouge&quot;&gt;-S&lt;/code&gt;) to the command you typed. So if we sum up we will have :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;git commit &lt;span class=&quot;nt&quot;&gt;-S&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Signing commit&quot;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To sign your tags (Releases) there is also an available option to do so :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;git tag &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; mytag &lt;span class=&quot;c&quot;&gt;# Creates a signed tag&lt;/span&gt;
git tag &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; mytag &lt;span class=&quot;c&quot;&gt;# Verifies the signed tag&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h1 id=&quot;-visual-results-in-github&quot;&gt;… Visual results in Github&lt;/h1&gt;

&lt;p&gt;After you commit, you can now push the changes to the upstream server (Github in our case). If you push to Github you will have the &lt;strong&gt;Verified&lt;/strong&gt; badge now being added to all of your commits :smile:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/posts/signed-commit.png&quot; alt=&quot;signed-commit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Is that not beautiful? It combines at the same time beauty and security!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Is that what we call black magic&lt;/em&gt;? 🤔&lt;/p&gt;

&lt;p&gt;Yes somehow ! You can consider cryptography signature as one !&lt;/p&gt;

&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;/h1&gt;

&lt;p&gt;Arrived at the end, we achieved our goal which was to have our commits signed using GPG (Gnu Privacy Guard) key. Note that even without doing all what we did in Github but doing those in &lt;strong&gt;Git&lt;/strong&gt; you will still have your commits signed and if you give a commit hash to someone with your public key he will be able to proove that you were or were not the one who did the commit. Github is just there to have a beautiful display (With the “verified” badge) of the result.&lt;/p&gt;

&lt;h1 id=&quot;references&quot;&gt;References&lt;/h1&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://help.github.com/en/articles/generating-a-new-gpg-key&quot;&gt;https://help.github.com/en/articles/generating-a-new-gpg-key&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://help.github.com/en/articles/signing-commits&quot;&gt;https://help.github.com/en/articles/signing-commits&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://www.wikiwand.com/en/Cryptography&quot;&gt;https://www.wikiwand.com/en/Cryptography&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;</content><author><name></name></author><category term="#git" /><category term="#gpg" /><category term="#versioncontrol" /><category term="#github" /><summary type="html">Have you ever wondered what the “verified” button near each commit hash in github/gitlab means? And maybe you want to have the same each time you commit? If you are curious like me and want to have it then here we go ! In this post I will guide you along the way on how to GPG-sign your commits using Git and Github (Not mandatory). Already excited ? Let’s go!</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://sdmg15.com/assets/images/posts/gpg.png.png" /></entry><entry><title type="html">:: 1,2,3 Hello World :tada: !</title><link href="https://sdmg15.com/blog/2019/02/10/hello-world/" rel="alternate" type="text/html" title=":: 1,2,3 Hello World :tada: !" /><published>2019-02-10T00:00:00+00:00</published><updated>2019-02-10T00:00:00+00:00</updated><id>https://sdmg15.com/blog/2019/02/10/hello-world</id><content type="html" xml:base="https://sdmg15.com/blog/2019/02/10/hello-world/">&lt;p&gt;Hello everybody ! Today I’m introducing my personnal blog.&lt;/p&gt;

&lt;h1 id=&quot;what-motivated-me&quot;&gt;What motivated me&lt;/h1&gt;

&lt;p&gt;I read a lot from blog posts, &lt;a href=&quot;https://news.ycombinator.com&quot;&gt;HN&lt;/a&gt; (Hacker News), &lt;a href=&quot;https://meduim.com&quot;&gt;Meduim&lt;/a&gt;, &lt;a href=&quot;https://dev.to&quot;&gt;Dev&lt;/a&gt; and maybe from your blog too :wink: , and while reading I come accross various interesting articles, I get always excited while reading them and appreciate how articles Authors took their time to write and explain with simple words sometimes a really tough concept.&lt;/p&gt;

&lt;p&gt;Writing about subject you think you know increase your knowledge on it actually, because you have to get sure 
about what you are writing, you’ll get yourself documented again in order to write a solid and consistent article or blog post.&lt;/p&gt;

&lt;p&gt;There’s also that feeling when you have constructive feedbacks and when your post has helped and saved day of some People/Developers or motivated them or at least had an impact on them, by that time you start feeling like a genius :smile:&lt;/p&gt;

&lt;p&gt;Writing I think is putting your knowledge into practice, putting your knowledge into practice to help others, putting your knowledge into practice to even verify if you know, putting your knowledge into practice for good !&lt;/p&gt;

&lt;p&gt;One last thing is that as a &lt;strong&gt;Software Engineer&lt;/strong&gt; Aspiring this is a good step to take, in order to show and describe my works and thoughts on various subjects to other people.&lt;/p&gt;

&lt;p&gt;Above are some of the facts not in a specific order that get me motivated; Thus the feeling of writing keeps animating me.&lt;/p&gt;

&lt;h1 id=&quot;conclusion-&quot;&gt;Conclusion …&lt;/h1&gt;

&lt;p&gt;So my point is that  writing is a skill that you cannot develop without writing, as the advise I gave in the &lt;a href=&quot;/about&quot;&gt;about&lt;/a&gt; page practicing is the key, and to get experienced at whatever you do, you have to practice, So I always follow that advise !&lt;/p&gt;

&lt;p&gt;I’m not an experienced writer, and you’ll never know how to write if you don’t actually do it !&lt;/p&gt;

&lt;p&gt;As I keep saying, learning is a really long journey, if you are ready to take the ship with me then… &lt;strong&gt;LET’S GO !&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks&lt;/em&gt;&lt;/p&gt;</content><author><name></name></author><category term="welcome" /><category term="beginning" /><category term="helloworld" /><summary type="html">Hello everybody ! Today I’m introducing my personnal blog.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://sdmg15.com/assets/images/posts/helloworld.jpeg" /></entry></feed>