Article

Being lazy in C++

dalvrosa ·Lobsters ·Published 2026-09-13

Old blog résumé Twitter Being lazy in C++ September 12, 2026 C++ Context Again, I haven’t posted in a long time. I suppose I’ll have to start all my new articles with this sentence, ahaha. I can say I was a bit lazy, and that’s a good…

From the article

Old blog résumé Twitter Being lazy in C++ September 12, 2026 C++ Context Again, I haven’t posted in a long time. I suppose I’ll have to start all my new articles with this sentence, ahaha. I can say I was a bit lazy, and that’s a good thing because we’re going to see how to be lazy in C++.

Lazy initialization is the practice of delaying initialization until it is needed. It can be used to optimize performance (which will be the subject of the next article), but it popped up when I was asked an interesting question a while ago while working for one of my clients.

int complex_computation() { std::cout << "Compute" << std::endl; return 2; } int main() { std::optional<int> a = 50; std::optional<int> b; std::cout << a.value_or(complex_computation()) << "\n"; std::cout <<…


Share this resource


Discovered 2026-09-14 Source Lobsters Archive 2026-09 →