21 new features of Modern C++ to use in your project
Tags: cpp, codenewbie, productivity, programming
Published: 2019-10-13 11:24:32

<!-- wp:paragraph -->
<p>So, you came across the Modern C++ &amp; overwhelmed by its features in terms of performance, convenience &amp; code expressiveness. But in a dilemma that how you can spot where you can enforce Modern C++ features in your day to day coding job. No worries, here we will see 21 new features of Modern C++ you can use in your project. </p>
<!-- /wp:paragraph -->

> /!\\\: Originally published @ [www.vishalchovatiya.com](http://www.vishalchovatiya.com).

<!-- wp:paragraph -->
<p>C++ community releasing new standards more frequently than iPhone releases. Due to this, C++ now becomes like an elephant and it is impossible to eat the whole elephant in one go. That is why I have written this post to kick start your Modern C++ journey. Here my intended audience is peeps who are moving from older(i.e. 98/03) C++ to Modern(i.e. 2011 onwards) C++. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I have chosen some of the Modern C++ features &amp; explained it with the minimalistic example to make you aware that how you can spot the places where you can employ new features.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>1. Digit separators</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int no = 1'000'000;                      // separate units like, thousand, lac, million, etc.
long addr = 0xA000'EFFF;                 // separate 32 bit address
uint32_t binary = 0b0001'0010'0111'1111; // now, explanation is not needed i guess</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Earlier you have to count digits or zeros, but now not anymore from C++14. </li><li>This will be useful while counting address in word, half-word or digit boundary or let say you have a credit card or social security number.</li><li>By grouping digits, your code would become more expressive.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>2. Type aliases</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">template &lt;typename T>
using dyn_arr = std::vector&lt;T>;
dyn_arr&lt;int> nums; // equivalent to std::vector&lt;int>

using func_ptr = int (*)(int);</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Semantically similar to using a&nbsp;<code>typedef</code>&nbsp;, however, type aliases are easier to read and are compatible with templates types also.  Thanks to C++11. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>3. User-defined literals </h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using ull = unsigned long long;

constexpr ull operator"" _KB(ull no)
{
    return no * 1024;
}

constexpr ull operator"" _MB(ull no)
{
    return no * (1024_KB);
}

cout&lt;&lt;1_KB&lt;&lt;endl;
cout&lt;&lt;5_MB&lt;&lt;endl;</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Most of the times you have to deal with real-world jargons like KB, MB, km, cm, rupees, dollars, euros, etc. rather defining functions which do the unit conversion on run-time, you can now treat it as user-defined literals as you do with other primitive types.</li><li>Very convenient for units &amp; measurement.</li><li>Adding constexpr will serve zero cost run-time performance impact which we will see later in this article &amp; I have written a more detailed article on it <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="http://www.vishalchovatiya.com/when-to-use-const-vs-constexpr-in-cpp/" target="_blank"><span style="color:#2a12e1" class="color">here</span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>4. Uniform initialization &amp; Non-static member initialization </h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Earlier, you have to initialize data members with its default values in the constructor or in the member initialization list. But from C++11, it’s possible to give normal class member variables (those that don’t use the&nbsp;<code>static</code>&nbsp;keyword) a default initialization value directly as shown below: </p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class demo
{
private:
    uint32_t m_var_1 = 0;
    bool m_var_2 = false;
    string m_var_3 = "";
    float m_var_4 = 0.0;

public:
    demo(uint32_t var_1, bool var_2, string var_3, float var_4)
        : m_var_1(var_1),
          m_var_2(var_2),
          m_var_3(var_3),
          m_var_4(var_4) {}
};

demo obj{123, true, "lol", 1.1};</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>This is more useful when there are multiple <a rel="noreferrer noopener" aria-label="sub-object (opens in a new tab)" href="http://www.vishalchovatiya.com/memory-layout-of-cpp-object/" target="_blank"><span style="color:#2504fa" class="color">sub-object</span></a>s defined as data members as follows:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class computer
{
private:
    cpu_t           m_cpu{2, 3.2_GHz};
    ram_t           m_ram{4_GB, RAM::TYPE::DDR4};
    hard_disk_t     m_ssd{1_TB, HDD::TYPE::SSD};

public:
    // ...
};</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>In this case, you do not need to initialize it in initializer list, rather you can directly give default initialization at the time of declaration.</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class X
{
    const static int m_var = 0;
};

// int X::m_var = 0; // not needed for constant static data members</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>You can also provide initialization at the time of declaration if members are <code>const</code> &amp; <code>static</code> as above.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>5. std::initializer_list </h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">std::pair&lt;int, int> p = {1, 2};
std::tuple&lt;int, int> t = {1, 2};
std::vector&lt;int> v = {1, 2, 3, 4, 5};
std::set&lt;int> s = {1, 2, 3, 4, 5};
std::list&lt;int> l = {1, 2, 3, 4, 5};
std::deque&lt;int> d = {1, 2, 3, 4, 5};

std::array&lt;int, 5> a = {1, 2, 3, 4, 5};

// Wont work for adapters
// std::stack&lt;int> s = {1, 2, 3, 4, 5};
// std::queue&lt;int> q = {1, 2, 3, 4, 5};
// std::priority_queue&lt;int> pq = {1, 2, 3, 4, 5};</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Assign values to containers directly by initializer list as do with C-style arrays.</li><li>This is also true for nested containers. Thanks to C++11. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>6. auto &amp; decltype</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">auto a = 3.14; // double
auto b = 1; // int
auto&amp; c = b; // int&amp;
auto g = new auto(123); // int*
auto x; // error -- `x` requires initializer</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li><code>auto</code>-typed variables are deduced by the compiler according to the type of their initializer. </li><li>Extremely useful for readability, especially for complicated types: </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// std::vector&lt;int>::const_iterator cit = v.cbegin();
auto cit = v.cbegin(); // alternatively

// std::shared_ptr&lt;vector&lt;uint32_t>> demo_ptr(new vector&lt;uint32_t>(0);
auto demo_ptr = make_shared&lt;vector&lt;uint32_t>>(0); // alternatively</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Functions can also deduce the return type using&nbsp;<code>auto</code>. In C++11, a return type must be specified either explicitly, or using&nbsp;<code>decltype</code>&nbsp;like: </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">template &lt;typename X, typename Y>
auto add(X x, Y y) -> decltype(x + y)
{
    return x + y;
}
add(1, 2);     // == 3
add(1, 2.0);   // == 3.0
add(1.5, 1.5); // == 3.0</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Defining return type as above called trailing return type i.e.  <code>-&gt; return-type</code>. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>7. Range-based for-loops </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Syntactic sugar for iterating over a container's elements. </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">std::array&lt;int, 5> a {1, 2, 3, 4, 5};
for (int&amp; x : a) x *= 2;
// a == { 2, 4, 6, 8, 10 }</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Note the difference when using&nbsp;<code>int</code>&nbsp;as opposed to&nbsp;<code>int&amp;</code>: </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">std::array&lt;int, 5> a {1, 2, 3, 4, 5};
for (int x : a) x *= 2;
// a == { 1, 2, 3, 4, 5 }</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>8. Smart pointers</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>C++11 introduces new smart(er) pointers:&nbsp;<code>std::unique_ptr</code>,&nbsp;<code>std::shared_ptr</code>,&nbsp;<code>std::weak_ptr</code>.&nbsp;</li><li>And <code>std::auto_ptr</code>&nbsp;now become deprecated and then eventually removed in C++17. </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">std::unique_ptr&lt;int> i_ptr1{new int{5}}; // Not recommendate
auto i_ptr2 = std::make_unique&lt;int>(5);  // More conviniently

template &lt;typename T>
struct demo
{
    T m_var;

    demo(T var) : m_var(var){};
};

auto i_ptr3 = std::make_shared&lt;demo&lt;uint32_t>>(4);</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>ISO CPP guidelines suggest avoiding the call of <code>new</code>&nbsp;and&nbsp;<code>delete</code>&nbsp;explicitly by the rule of <a rel="noreferrer noopener" aria-label="no naked new (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-new" target="_blank"><span style="color:#1632ea" class="color">no naked new</span></a>.</li><li>I have written a more detailed article on smart pointers <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="http://www.vishalchovatiya.com/understanding-unique-ptr-with-example-in-cpp11/" target="_blank"><span style="color:#2e07ef" class="color">here</span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>9. nullptr </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>C++11 introduces a new null pointer type designed to replace C's&nbsp;<code>NULL</code>&nbsp;macro.</li><li><code>nullptr</code>&nbsp;itself is of type&nbsp;<code>std::nullptr_t</code>&nbsp;and can be implicitly converted into pointer types, and unlike&nbsp;<code>NULL</code>, not convertible to integral types except&nbsp;<code>bool</code>. </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">void foo(int);
void foo(char*);
foo(NULL); // error -- ambiguous
foo(nullptr); // calls foo(char*)</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>10. Strongly-typed enums</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">enum class STATUS_t : uint32_t
{
    PASS = 0,
    FAIL,
    HUNG
};

STATUS_t STATUS = STATUS_t::PASS;
STATUS - 1; // not valid anymore from C++11</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Type-safe enums that solve a variety of problems with C-style enums including implicit conversions, arithmetic operations, inability to specify the underlying type, scope pollution, etc. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>11. Typecasting</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>C style casting only change the type without touching underlying data. While older C++ was a bit type-safe and has a feature of specifying type conversion operator/function. But it was implicit type conversion, from C++11,  conversion functions can now be made explicit using the&nbsp;<code>explicit</code>&nbsp;specifier as follows. </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">struct demo
{
    explicit operator bool() const { return true; }
};

demo d;
if (d);                             // OK calls demo::operator bool()
bool b_d = d;                       // error: cannot convert 'demo' to 'bool' in initialization
bool b_d = static_cast&lt;bool>(d);    // OK, explicit conversion, you know what you are doing</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>If the above code looks alien to you, I have written a more detailed article on <a rel="noreferrer noopener" aria-label="C++ typecasting here (opens in a new tab)" href="http://www.vishalchovatiya.com/cpp-type-casting-with-example-for-c-developers/" target="_blank"><span style="color:#340abd" class="color">C++ typecasting here</span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>12. Move semantics</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>When an object is going to be destroyed or unused after expression execution, then it is more feasible to move resource rather than copying it.  </li><li>Copying includes unnecessary overheads like memory allocation, deallocation &amp; copying memory content, etc. </li><li>Consider the following swap function:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">template &lt;class T>
swap(T&amp; a, T&amp; b) {
    T tmp(a);   // we now have two copies of a
    a = b;      // we now have two copies of b (+ discarded a copy of a)
    b = tmp;    // we now have two copies of tmp (+ discarded a copy of b)
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>using move allows you to swap the resources instead of copying them around: </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">template &lt;class T>
swap(T&amp; a, T&amp; b) {
    T tmp(std::move(a));
    a = std::move(b);   
    b = std::move(tmp);
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Think of what happens when&nbsp;<code>T</code>&nbsp;is, say,&nbsp;<code>vector&lt;int&gt;</code>&nbsp;of size n. And n is too big. </li><li>In the first version, you read and write 3*n elements, in the second version you basically read and write just the 3 pointers to the vectors' buffers, plus the 3 buffers' sizes. </li><li>Of course, class&nbsp;<code>T</code>&nbsp;needs to know how to do the moving; your class should have a <a rel="noreferrer noopener" aria-label="move-assignment operator and a move-constructor (opens in a new tab)" href="http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/" target="_blank"><span style="color:#2309cf" class="color">move-assignment operator and a move-constructor</span></a> for class&nbsp;<code>T</code>&nbsp;for this to work. </li><li>This feature will give you a significant boost in the performance which is why people use C++ for(i.e. last 2-3 drops of speed).</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>13. Forwarding references</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Also known (unofficially) as&nbsp;<em>universal references</em>. A forwarding reference is created with the syntax&nbsp;<code>T&amp;&amp;</code>&nbsp;where&nbsp;<code>T</code>&nbsp;is a template type parameter, or using&nbsp;<code>auto&amp;&amp;</code>. This enables two major features<ul><li>move semantics</li><li>And&nbsp;<em><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.cppreference.com/w/cpp/utility/forward#Example" target="_blank"><span style="color:#1f0bf3" class="color">perfect forwarding</span></a></em>, the ability to pass arguments that are either lvalues or rvalues.</li></ul></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Forwarding references allow a reference to binding to either an lvalue or rvalue depending on the type. Forwarding references follow the rules of&nbsp;<em>reference collapsing</em>:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><code>T&amp; &amp;</code>&nbsp;becomes&nbsp;<code>T&amp;</code></li><li><code>T&amp; &amp;&amp;</code>&nbsp;become&nbsp;<code>T&amp;</code></li><li><code>T&amp;&amp; &amp;</code>&nbsp;becomes&nbsp;<code>T&amp;</code></li><li><code>T&amp;&amp; &amp;&amp;</code>&nbsp;becomes&nbsp;<code>T&amp;&amp;</code></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Template type parameter deduction with lvalues and rvalues:</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// Since C++14 or later:
void f(auto&amp;&amp; t) {
  // ...
}

// Since C++11 or later:
template &lt;typename T>
void f(T&amp;&amp; t) {
  // ...
}

int x = 0;
f(0); // deduces as f(int&amp;&amp;)
f(x); // deduces as f(int&amp;)

int&amp; y = x;
f(y); // deduces as f(int&amp; &amp;&amp;) => f(int&amp;)

int&amp;&amp; z = 0; // NOTE: `z` is an lvalue with type `int&amp;&amp;`.
f(z); // deduces as f(int&amp;&amp; &amp;) => f(int&amp;)
f(std::move(z)); // deduces as f(int&amp;&amp; &amp;&amp;) => f(int&amp;&amp;)</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>If this seems complex &amp; weird to you then <a rel="noreferrer noopener" aria-label="read this first (opens in a new tab)" href="http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/" target="_blank"><span style="color:#3607ef" class="color">read this first</span></a> &amp; then come back here.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>14. Variadic templates </h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">void print() {}

template &lt;typename First, typename... Rest>
void print(const First &amp;first, Rest &amp;&amp;... args)
{
    std::cout &lt;&lt; first &lt;&lt; std::endl;
    print(args...);
}

print(1, "lol", 1.1);</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>The&nbsp;<code>...</code>&nbsp;syntax creates a&nbsp;<em><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.cppreference.com/w/cpp/language/parameter_pack" target="_blank"><span style="color:#1f15de" class="color">parameter pack</span></a></em>&nbsp;or expands one. A template&nbsp;<em>parameter pack</em>&nbsp;is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A template with at least one parameter pack is called a&nbsp;<em>variadic template</em>. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>15. constexpr</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">constexpr uint32_t fibonacci(uint32_t i)
{
    return (i &lt;= 1u) ? i : (fibonacci(i - 1) + fibonacci(i - 2));
}

auto fib_5th_term = fibonacci(6); // equal to `auto fib_5th_term = 8`
</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Constant expressions are expressions evaluated by the compiler at compile-time. In the above case, <code>fibonacci</code> the function is executed/evaluated by the compiler at the time of compilation &amp; result will be substituted at calling the place. </li><li>I have written a detailed article on <a rel="noreferrer noopener" aria-label="when to use const vs constexpr in C++ (opens in a new tab)" href="http://www.vishalchovatiya.com/when-to-use-const-vs-constexpr-in-cpp/" target="_blank"><span style="color:#1220ef" class="color">when to use const vs constexpr in C++</span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>16. Deleted &amp; Defaulted functions</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">struct demo
{
    demo() = default;
};

demo d;</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Now you might be wondering that rather than writing 8+ letters(i.e. <code>= default;</code>), I could simply use {} i.e. empty constructor. That's true! but think about <a rel="noreferrer noopener" aria-label="copy constructor (opens in a new tab)" href="http://www.vishalchovatiya.com/all-about-copy-constructor-in-cpp/" target="_blank"><span style="color:#2513e9" class="color">copy constructor</span></a>, <a rel="noreferrer noopener" aria-label="copy assignment operator (opens in a new tab)" href="http://www.vishalchovatiya.com/2-wrong-way-to-learn-copy-assignment-operator-in-c/" target="_blank"><span style="color:#1a07f1" class="color">copy assignment operator</span></a>, etc. </li><li>An empty <a rel="noreferrer noopener" aria-label="copy constructor (opens in a new tab)" href="http://www.vishalchovatiya.com/all-about-copy-constructor-in-cpp/" target="_blank">copy constructor</a>, for example, will not do the same as a defaulted c<a rel="noreferrer noopener" aria-label="opy constructor (opens in a new tab)" href="http://www.vishalchovatiya.com/all-about-copy-constructor-in-cpp/" target="_blank">opy constructor</a> (which will perform a member-wise copy of its members). </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>You can limit certain operation or way of <a rel="noreferrer noopener" aria-label="object instantiation (opens in a new tab)" href="http://www.vishalchovatiya.com/inside-the-c-object-model/" target="_blank"><span style="color:#0905fb" class="color">object instantiation</span></a> by simply deleting the respective method as follows</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class demo
{
    int m_x;

public:
    demo(int x) : m_x(x){};
    demo(const demo &amp;) = delete;
    demo &amp;operator=(const demo &amp;) = delete;
};

demo obj1{123};
demo obj2 = obj1; // error -- call to deleted copy constructor
obj2 = obj1;      // error -- operator= deleted</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>In older C++ you have to make it private. But now you have <code>delete</code> compiler directive.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>17. Delegating constructors</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">struct demo
{
    int m_var;
    demo(int var) : m_var(var) {}
    demo() : demo(0) {}
};

demo d;</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>In older C++, you have to create common initialization member function &amp; need to call it from all the constructor to achieve the common initialization.</li><li>But from C++11, now constructors can call other constructors in the same class using an initializer list. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>18. Lambda expression</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">auto generator = [i = 0]() mutable { return ++i; };
cout &lt;&lt; generator() &lt;&lt; endl; // 1
cout &lt;&lt; generator() &lt;&lt; endl; // 2
cout &lt;&lt; generator() &lt;&lt; endl; // 3</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>I think this feature no need any introduction &amp; hot favourite among other features.</li><li>Now you can declare functions wherever you want. That too with zero cost performance impact.&nbsp;</li><li>I wrote a separate article to <a rel="noreferrer noopener" aria-label="learn lambda expression in C++ with example (opens in a new tab)" href="http://www.vishalchovatiya.com/learn-lambda-function-in-cpp-with-example/" target="_blank"><span style="background-color:#ffffff" class="background-color"><span style="color:#1e13f0" class="color">learn lambda expression in C++ with example</span></span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>19. Selection statements with initializer</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>In earlier C++, the initializer is either declared before the statement and leaked into the ambient scope, or an explicit scope is used. </li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html" target="_blank"><span style="color:#0625f3" class="color">With C++17, the new form of </span></a><code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html" target="_blank"><span style="color:#0625f3" class="color">if/switch</span></a></code> can be written more compactly, and the improved scope control makes some erstwhile error-prone constructions a bit more robust: </li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">switch (auto STATUS = window.status()) // Declare the object right within selection statement
{
case PASS:// do this
    break;
case FAIL:// do that
    break;
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>How it works</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
    auto STATUS = window.status();
    switch (STATUS)
    {
    case PASS: // do this
        break;
    case FAIL: // do that
        break;
    }
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>20. std::tuple </h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">auto employee = std::make_tuple(32, " Vishal Chovatiya", "Bangalore");
cout &lt;&lt; std::get&lt;0>(employee) &lt;&lt; endl; // 32
cout &lt;&lt; std::get&lt;1>(employee) &lt;&lt; endl; // "Vishal Chovatiya"
cout &lt;&lt; std::get&lt;2>(employee) &lt;&lt; endl; // "Bangalore"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Tuples are a fixed-size collection of heterogeneous values. Access the elements of a&nbsp;<code>std::tuple</code>&nbsp;by unpacking using&nbsp;<code>std::tie</code>, or using&nbsp;<code>std::get</code>. </li><li>You can also catch arbitrary &amp; heterogeneous return values as follows:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">auto get_employee_detail()
{
    // do something . . . 
    return std::make_tuple(32, " Vishal Chovatiya", "Bangalore");
}

string name;
std::tie(std::ignore, name, std::ignore) = get_employee_detail();</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Use&nbsp;<code>std::ignore</code>&nbsp;as a placeholder for ignored values. In C++17, <a rel="noreferrer noopener" aria-label="structured bindings (opens in a new tab)" href="https://stackoverflow.com/questions/40673080/stdignore-with-structured-bindings" target="_blank"><span style="color:#4a14de" class="color">structured bindings</span></a> should be used instead. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>21. Class template argument deduction</h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp","linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">std::pair&lt;std::string, int> user = {"M", 25}; // previous
std::pair user = {"M", 25};                   // C++17

std::tuple&lt;std::string, std::string, int> user("M", "Chy", 25); // previous
std::tuple user2("M", "Chy", 25);                               // deduction in action!</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Automatic template argument deduction much likes how it's done for functions, but now including class constructors as well. </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Closing words</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here, we have just scratched the surface in terms of <a rel="noreferrer noopener" aria-label="new feature (opens in a new tab)" href="https://github.com/AnthonyCalandra/modern-cpp-features" target="_blank"><span style="color:#1a07eb" class="color">new feature</span></a> &amp; the possibility of its application. There are many things to learn in Modern C++, but still, you can consider this as a good starting point. Modern C++ is not only expanding in terms of syntax but there is lot more other features are also added like unordered containers, threads, regex, Chrono, random number generator/distributor and many new STL algos(like <code>all_of()</code>,&nbsp;<code>any_of()</code>&nbsp;and&nbsp;<code>none_of()</code>, etc).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Happy Modern C++ Coding...! </p>
<!-- /wp:paragraph -->

## Other related articles

- [All about lambda function in C++](https://dev.to/visheshpatel/all-about-lambda-function-in-c-j4e)
- [C++ type casting with example for C developers](https://dev.to/visheshpatel/c-type-casting-with-example-for-c-developers-29b8)
- [How floating-point no is stored in memory?](https://dev.to/visheshpatel/how-floating-point-no-is-stored-memory-47od)