The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

sanjaykhan (4) [Avatar] Offline
In listing 6.7 ("Function that creates a memoized wrapper from a function pointer"), should the cache variable be static?
Ivan Cukic (104) [Avatar] Offline
No. The lifetime of the cache is tied to the lifetime of the lazy variable.

There are a few solutions that use a static cache, which I'll provide links to, but it is not something that this implementation needs.
Jan (1) [Avatar] Offline
Suppose I have the implementation of an expensive free function used all over my code in some source file.
How can I use make_memoized without changes outside of said source file?
My naive implementations give me a std::bad_alloc.
Ivan Cukic (104) [Avatar] Offline

If you had to preserve the API/ABI, you'd can create a static variable and a std::once_flag inside the function and use std::call_once to initialize said variable.

If this does not work for you, can you post the code you have the problem with?