Jason Turner: IIFE in C++ for Performance and Safety

preview_player
Показать описание
IIFE (Immediately-Invoked Function Expression) is a common tool used in JavaScript. The idea is to both define an anonymous function and call it in the same expression. The point is to produce a local scope for variables so they do not pollute the global scope.

This same technique can be deployed in C++ to lead to cleaner, safer, more performant code when building up objects which require multiple steps to initialize.

---

*--*

---
Рекомендации по теме
Комментарии
Автор

I dont know the performance implications but isnt the ternary operator a viable alternative at least for simple cases? The simple if-else assignment example at the beginning is pretty much exactly the example given for the operator on cplusplus.com

ennokoster
Автор

Doesn't IIFE Lambda has more cost than const initialisation?

vivekatbitm
Автор

I think the ternary is more readable and I find tiny functions that do one thing fine. 95% of the time I see this being used it feels like a solution looking for a problem.

jenius