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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

if (auto var = foo(); condition) {
    ...
}
else{
    ...
}
  
// Another if-else block
if (auto var = bar(); condition) {
    ....
}
else {
    ....
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
  }  
}

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.