C++17 new feature : If Else and Switch Statements with initializers - GeeksforGeeks

27 Jan, 2018
In many cases, we need to check the value of something returned by a function and perform conditional operations based on this value. So our code looks something like this
// Some functionreturn_type foo(params)  // Call function with params and // store return in varauto var = foo(params);  if (var == /* some value */) {    //Do Something} else {   //Do Something else}
While this is alright, notice that there are a few subtleties
The variable leaks into the surrounding scope, which is not intended and care must be taken to ensure that this variable doesn’t get mixed up with other variables
Since this variable has already been defined, if we were to call another function (say bar()) later in the program we would need to create another variable to store the values returned from bar()
Thirdly, if the compiler can know explicitly that the variable is going to be used only in one if-else block then it may be able to better optimize the code.
Notice the general format in all conditional if-else blocks. Firstly there is an optional initial statement which sets up the variable, followed by the if-else block. So the general if-else block resembles as follows
init-statement

if (condition) {
    // Do Something
} else {
   // Do Something else 
}

In C++17 the init statement is called an initializer, and we can directly put it into the if-else block as follows
if (init-statement; condition) {
    // Do Something
} else {
    // Do Something else
}

The scope of the conditioned variable gets limited to the current if-else block. This also allows us to reuse the same named identifier in another conditional block.
if (auto var = foo(); condition) {    ...}else{    ...}  // Another if-else blockif (auto var = bar(); condition) {    ....}else {    ....}
Likewise the switch case block has been updated. We can now put an initial expression inside the switch parenthesis. After the initial statement we need to specify which variable is being used to check the cases
switch (initial-statement; variable) {
    ....
    // cases
}

A complete program
// Program to demonstrate init if-else// feature introduced in C++17#include <iostream>#include <cstdlib>using namespace std;  int main() {    // Set up rand function to be used  // later in program  srand(time(NULL));    // Before C++17  int i = 2;  if ( i % 2 == 0)    cout << i << " is even number" << endl;    // After C++17  // if(init-statement; condition)  if (int i = 4; i % 2 == 0  )    cout << i << " is even number" << endl;    // Switch statement  // switch(init;variable)  switch (int i = rand() % 100; i) {      default:          cout << "i = " << i << endl; break;  }  }
Output (Machine Dependent)
2 is even number
4 is even number
i = 5

Note: To compile these programs we need latest versions of compilers. As of writing this article this feature has been completely implemented in clang 5 and gcc 7 onwards. To compile the programs we also need to specify the -std=c++17 flag
g++-7 program_file.cpp -std=c++17

or

clang++ program_file.cpp -std=c++17

cpp-advanced
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Inline Functions in C++
Queue in C++ Standard Template Library (STL)
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++