Skip to content
Related Articles

Related Articles

C++17 new feature : If Else and Switch Statements with initializers
  • Difficulty Level : Medium
  • Last Updated : 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 function
return_type foo(params)
  
// Call function with params and 
// store return in var
auto 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 block
if (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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :