Better HN
topnewbestaskshowjobs
Log inSign up

tunah2

9 karma · joined Mar 28, 2016 (10y ago) · 2 submissions

Submissions

comment 10y ago · link · parent

Because kids do better when their parents take care of them, and "programs" can't replace this.

Companies have to help out because they're part of society. In exchange they get to do things like limit their liability and collectively enter into contracts.

comment 10y ago · link · parent

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...
  }