New feature of C++17

C++Server Side ProgrammingProgramming

The C++ Standards committee is always focusing on shipping new features every three years. The two main parts of the specification are the core functionality of the programming language and the Standard Template Library (STL). The new features are introduced to make the code cleaner, easier and compact. Following is the list of features that are introduced-:

1. Fold Expressions

Fold expressions are used to write shorter codes for a variable number of arguments that can be passed to a function or can be returned from the function. It enables the use of any number of variables as arguments and in return statements of a function.

Syntax:-

  • Unary right fold - ( pack op1 ... )

  • Unary left fold - ( … op1 pack )

  • Binary left fold - ( init op1 … op1 pack )

  • Binary right fold - ( pack op1 … op1 init )

Here pack is a parameter pack that can be expanded for any number of variables. op1 is an operator. ( -, + , <=, >=, <, > , ==, *, / …. ). In binary folds, both op1 are the same operators.

init is an expression that cannot be expanded.

Example

#include <iostream>
#include <string>
using namespace std;
template<typename ...Args> auto addition(Args ...args){
   return (args + ... + 0);
}
template<typename ...Args> auto sum2(Args ...args){
   return (args + ...);
}
int main(){
   cout << "Sum is : "<<addition(1,1,1,1,1) << endl;
   cout << "Sum 2 is : "<<addition ( 1,2,3);
}

Output

Sum is : 5
Sum 2 is : 6

2. Structure Bindings

These are used to declare multiple variables to be initialized with values in a pair, tuple etc. All these binding of variables with initializers done in a single statement.

  • Case 1:- binding an array

    Each identifier in the identifier list becomes the name of lvalue for element of array. Number of elements must be equal to the number of identifiers.

    int arry[3] = { 3,4,5 };

    auto [a,b,c] = arry;

    //here array is created and a refers to 3, b refers to 4 and c refers to 5.

  • Case 2:- binding a tuple like type

    float fnum{};

    char ch1{};

    int number{};

    std::tuple < float&, char&&, int > tplex( fnum, std::move(ch1) , number);

    const auto& [ p, q, r] = tplex;

    // p is name of structured binding referring to fnum

    // q is name of structured binding referring to ch1

    // r is name of structured binding referring to number

  • Case 3:- binding to data members

    struct structVar {

    mutable int num1 : 2;

    volatile double num2;

    };

    structVar func();

    const auto [ a, b] = func();

    // a is an int lvalue for the 2-bit bit field

    // b is a const volatile double lvalue

3. Initialization of enums using Direct List

With C++17 the enums can now be initialized using braces.

Syntax:-

enum byte : unsigned char {};
byte b0 {0}; // OK
byte b1 = byte{1}; // OK
byte b2 = byte{256}; // ERROR - 0 to 255 only

4. Variable declaration inside If and Switch

C++17 allows declaration of variables inside if and switch conditions. This makes it easy to use variables with same names that have different scopes.

Syntax:-

if (data type variable condition)
{
   //statements
}
switch ( condition; variable )
{
   //statements
}

5. If constexpr statement

Useful feature for template codes. The if constexpr statement is evaluated at compile time.

How it is

Helpful can be shown using comparisons below:-

General If-else statements:-

int var = 10;
if (var >= 10) {
   var=var+10;
} else {
   var=var-10;
}

Constexpr If-else statements:-

template <typename T>
auto length ( T const& value ) { 
   //checking if T is integer or not
   if (is_integral<T>::value) {
      return value;
   } else {
      return value.length();
   }
}

6. Nested Namespaces

Namespaces are used to group together similar codes like classes and functions that are correlated. C++17 allows more easy syntax of using nested namespaces. Earlier the syntax was quite messy when the number of nested namespaces was more. Handling braces is now no longer required.

Before C++17:-

namespace Earth{

   namespace Continent {
      namespace Country {
         class City {
         ..........
}; } } }

New Syntax:-

namespace Earth :: Continent :: Country {
   class City {
      ..........
}; }
raja
Updated on 22-Oct-2021 09:07:35

Advertisements