News for hackers. Stuff that matters.
Re:Under the hood of C++ lambdas and std::function by tunah2 on Monday March 28, 2016 @11:58AM (#11375007)
Lazy initialization is one example:

  void foo() {
    // initialized once when foo is first called
    static auto lazy_data = ...;

    // use lazy_data...
  }
As of c++11, the compiler is required to make this threadsafe (initializer only runs once, even if foo() is called concurrently), typically by inserting locks.

Since this only applies to the initializer, for complex initialization a self-executing lambda can be used:

  void foo() {
    static auto lazy_data = []{
      auto data = new Whatever();
      // initialize data...
      return data;
    }();

    // use lazy_data...
  }

There are no comments on this comment yet.