C++ · 7 · Advanced / Job-Ready30 / 36 · 83%
Lambdas, constexpr & Exceptions
The idioms that show up in every real-world modern C++ codebase.
shortcuts: ← prev · → next · M mark
1
Lambdas
Anonymous functions with a capture list.
Example
example
int threshold = 10;
auto over = [threshold](int x) { return x > threshold; };
std::vector<int> v{1, 20, 3, 40};
auto n = std::count_if(v.begin(), v.end(), over); // 22
constexpr
Compute at compile time — zero runtime cost.
Example
example
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
static_assert(factorial(5) == 120);3
Exceptions
Throw / try / catch — pair with RAII for safe cleanup.
Example
example
void load(const std::string& path) {
std::ifstream f(path);
if (!f) throw std::runtime_error("cannot open " + path);
// ...
}
try { load("data.txt"); }
catch (const std::exception& e) {
std::cerr << "err: " << e.what() << '\n';
}4
C++20 Modules
The modern replacement for #include — faster builds, real encapsulation.
Example
example
// math.ixx
export module math;
export int add(int a, int b) { return a + b; }
// main.cpp
import math;
int main() { return add(2, 3); }NOTE
Modules avoid re-parsing headers per translation unit. Big compile-time wins.