C++ - Immediately Invoked Lambda Expression

less than 1 minute read

Immediately Invoked Lambda Expression technique on initialize the singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
IPersistentProvider* instance = null

IPersistentProvider& get_provider() {
  static bool init = [] {
    if (!instance) {
      static SpecificProvider provider;
      instance = &provider;
    }
    return true;
  }();

  return instance;
}

Categories:

Updated: