Sitemap
Life EXE

Life EXE

My name is Yuri. I've been in software development for over 15 years. Area of ​​interest: game dev, c++, computer vision, dev ops, tracking, CG, VR/XR/AR

Immediately Invoked Lambda Expression (IILE)

2 min readApr 13, 2025

--

Today on the menu is an obvious, but not always applied approach: Immediately Invoked Lambda Expression (IILE)

Press enter or click to view image in full size

Why is const important?

Immutability is key to reliable and understandable code. Using const wherever possible helps the compiler catch errors and shows the programmer's intent more clearly. When another person reads your code and sees const, it reduces their cognitive load – "no need to track changes to this value – relax". Const orrectness is an important practice in C++.

The Problem

In simple cases, initializing constants isn’t a problem:

const int c_maxPlayers = 100;
const double c_scale = getScaleFactor() * 1.5;
const bool c_enabled = check() || FORCE_ENABLE;
const int c_healthModifier = bHealing ? 20 : 0;

But what if calculating the value for a constant requires multiple steps, temporary variables, loops, or conditions?

float c_calculatedDamage = getBaseDamageValue();
if (targetAimed(calculatedDamage)) {
for (int i = 0; i < c_effectCount; ++i) {
calculatedDamage += getBonusDamage(i);
}
}

Traditional approaches — extracting the logic into a separate named function or dropping const – are not always ideal. Creating a separate function can be overkill if the logic is used only once. Dropping const reduces code safety and expressiveness.

Immediately Invoked Lambda Expression (IILE)

This is where using an Immediately Invoked Lambda Expression (IILE) comes to the rescue. We define a lambda function that encapsulates all the complex initialization logic, and then immediately call it. The result of this call is assigned to our constant.

Get Life EXE’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s how it looks:

const auto myLambda = [](){ return 13; }();

It’s all about the parentheses 🤪 The final parentheses () after the lambda's curly braces are the immediate invocation. They make the lambda execute right at the point of definition. This means we can initialize a complex object and preserve constness:

const auto c_calculatedDamage = [&]() {
float tempDamage = getBaseDamageValue();
if (targetAimed(tempDamage)) {
for (int i = 0; i < c_effectCount; ++i) {
tempDamage += getBonusDamage(i);
}
}
return tempDamage;
}();

Advantages of IILE for Initialization

🟢 Encapsulation — All initialization logic is gathered in one place.
🟢 Locality — Temporary variables used for calculation don’t “pollute” the outer scope.
🟢 Const correctness — Allows declaring the variable as const (or even constexpr, if the lambda meets the requirements), even if its calculation is multi-step.
🟢 Code Cleanliness — Eliminates the need to create separate, single-use named functions.

Alternative Syntax (C++17)

In C++17, you can use std::invoke, although for IILE, the direct call () is usually preferred and clearer:

#include <functional>
// ...
const auto c_anotherConstant = std::invoke([] {
// ... logic ...
return 13;
});

References

🔗 ES.28: Use lambdas for complex initialization, especially of const variables

--

--

Life EXE
Life EXE

Written by Life EXE

My name is Yuri. I've been in software development for over 15 years. Area of ​​interest: game dev, c++, computer vision, dev ops, tracking, CG, VR/XR/AR