Avatar
Gameplay/AI programmer.
Write about game programming, Unreal Engine and C++

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 an 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

“New” if statement looks like that:

if (init; condition)

Guard clause

Here is an 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 shorter. It is just an if statement with simple condition which is permitted to be:

declaration of a single non-array variable with a brace-or-equals initializer

Techically, there is an implicit condition. But it is not an initializer with a coniditonal! You can use guard clause in previous versions of C++.

Initializer with condition

For C++17 if statement with initializer, you must 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 that is your responsibility to perform nullptr checks, because first statement is not a condition anymore, it is just an initializer.

Remember, that conditional scope accept any condition, so you can combine it any way you want.

Conclusion

Personally, using features provided by technology is an absolutely good practice. Being up-to-date is a good practice as well. To conclude, if statement with initializers will make your code more modern and cleaner.