If statement with initializer
What it is
This feature was added in C++17. What does it mean for us? We can initialize a variable inside if statement, and perform the actual conditional check after initialization.
Statement | Equivalent | Iterations |
---|---|---|
if (init; cond) E; | { init; while(cond) { E; break; } } | Once while cond holds |
if (init; cond) E; else F; | (more complex) | Once |
Tehnically, new if looks like:
if (init; condition)
Guard clause
Here is example of guard clause:
AMyPlayerCharacter* PC = UMyFunctionLibrary::GetMyPlayerCharacter(this);
if (PC)
{
PC->DoStuff();
}
if (AMyPlayerCharacter* PC = UMyFunctionLibrary::GetMyPlayerCharacter(this))
{
PC->DoStuff();
}
Second one is one-line less. It is just if with simple condition, because condition is permitted to be:
declaration of a single non-array variable with a brace-or-equals initializer
So techically, there is implicit condition. But it is not initializer with coniditonal! And you can use it in previous versions of C++.
Initializer with condition
If you want use C++17’s if statement with initializer, you should provide explicit condition:
if (int32 RandomNumber = FMath::RandRange(0, 100); RandomNumber < MinimalChance)
{
DoRandomStuff();
}
Also, you can combine guard clause with condition:
if (AMyPlayerCharacter* PC = UMyFunctionLibrary::GetMyPlayerCharacter(this);
PC && PC->CanDoStuff())
{
PC->DoStuff();
}
Note, you should care about checking on nullptr
. Because first statement is not condition anymore, it is just initializer.
Remember, that conditional scope accept any condition, so you can combine it any way you would like to.
Conclusion
For me, using features provided by technology is absolutely good practice. And be up-to-date is a good practice too.
So using if statement with initializers
will make your code cleaner and more modern.