Lazy versus eager evaluation

Lazy evaluation is a technique used to postpone an operation until its result is really required. The opposite, where operations are performed right away, is called eager evaluation. In some situations eager evaluation is undesired as we might end up constructing a value which is not utilized.

Take a look at the following function class corresponding to an audio library. It consists of two functions for retrieving an audio file by name, get_eager() in which we pass an audio file if the name cannot be found, and get_lazy() in which we pass a function which returns an audio file if the name cannot be found:

struct Audio {}; 
auto load_audio(const std::string& path) -> Audio {...}; class AudioLibrary {public: auto ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.