Modern C++ idoms and recipes

meshell
58K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Init Statements for if/switch

In the previous exercise you probably had the problem, that you had to rename some variables or had to put it in a different scope to have const correctness. E.g

{
  const auto it = address_book.find("Hans");
  if (it != address_book.end()) {
    const auto& [name, address] = *it;
    std::cout << name << " is in address book living in " << address.city << std::endl;
  } else {
    std::cout << "Hans" << " is not in address book\n";
  }
}
{
  const auto it = address_book.find("Peter");
  if (it != address_book.end()) {
    const auto& [name, address] = *it;
    std::cout << name << " is in address book living in " << address.city << std::endl;
  } else {
    std::cout << "Peter" << " is not in address book\n";
  }
}

With C++17 there are new versions of the if and switch statements: if (init; condition) and switch (init; condition).

The new if statement will make that additional scope in the example above obsolete and the code shorter:

if (const auto it = address_book.find("Hans"); it != address_book.end()) {
  const auto& [name, address] = *it;
  std::cout << name << " is in address book living in " << address.city << std::endl;
} else {
  std::cout << "Hans" << " is not in address book\n";
}

if (const auto it = address_book.find("Peter"); it != address_book.end()) {
  const auto& [name, address] = *it;
  std::cout << name << " is in address book living in " << address.city << std::endl;
} else {
  std::cout << "Peter" << " is not in address book\n";
}

DIY

Refactor the code and use the new if form to reduce the scope of the constants
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content