Daily Unreal Column #2 - ON_SCOPE_EXIT
Some modern programming languages like Odin or Go implement a feature called deferred code execution. C++ is not one of them. Luckily, Unreal Engine has it's own solution to this.
Deferred code executions means that specified part of code is going to be evaluated when the program is going to be exiting the scope its defined in. The name of the macro that performs this function is ON_SCOPE_EXIT
. Here is a code snippet using it:
#include <iostream>
int main()
{
ON_SCOPE_EXIT
{
std::cout << "deferred text" << std::endl;
}
std::cout << "normal text" << std::endl;
return 0;
}
The output of the above program looks like following:
normal text
deferred text
The code defined inside the ON_SCOPE_EXIT
’s scope it being lazily evaluated and executed when the program is exiting the main
function’s scope.
at the end of the ON_SCOPE_EXIT scope you need to add a semicolon.
ON_SCOPE_EXIT
{
std::cout << "deferred text" << std::endl;
};