Skip to content

Latest commit

 

History

History
114 lines (97 loc) · 1.1 KB

if_init.md

File metadata and controls

114 lines (97 loc) · 1.1 KB

if-init

C++
{
   if (Foo * ptr = get_foo())
      use(*ptr);
   more_code();
}

But what do you do when it isn't (convertible to) boolean?

C++
{
   {
      QVariant var = getAnswer();
      if (var.isValid())
         use(var);
   }
   more_code();
}
C++ C++17
{
   {
      QVariant var = getAnswer();
      if (var.isValid())
         use(var);
   }
   more_code();
}
{
   
   
   if (QVariant var = getAnswer(); var.isValid())
      use(var);
      
   more_code();
}

Switch statements too!

C++17
{
   switch (Device dev = get_device(); dev.state())
   {
   case sleep: /*...*/ break;
   case ready: /*...*/ break;
   case bad: /*...*/ break;
   }
}