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