News for hackers. Stuff that matters.
Re:Under the hood of C++ lambdas and std::function by Animats on Monday March 28, 2016 @02:51AM (#11373065)
From the article: "My favorite expression in C++ is [](){}();, which declares an empty lambda and immediately executes it."

Sigh.

Re:Under the hood of C++ lambdas and std::function by tomjakubowski on Monday March 28, 2016 @07:12AM (#11373603)
If you'll excuse me while I golf line noise, you can just write it as []{}(), too; the parameter list may be omitted for a lambda that takes no arguments.
Re:Under the hood of C++ lambdas and std::function by igravious on Monday March 28, 2016 @04:52AM (#11373289)
Come on. What's wrong with that? That tickles me pink both aesthetically and logically? Expand on "sigh" sir!
Re:Under the hood of C++ lambdas and std::function by Kristine1975 on Monday March 28, 2016 @07:31AM (#11373654)
Then you'll be delighted at seeing this function:

  template <typename... T>
  void foo(T......);
Re:Under the hood of C++ lambdas and std::function by Too on Monday March 28, 2016 @03:58AM (#11373190)
In JavaScript it's quite common to see self executing lambdas like this (not empty though), because there variables have function scope rather than block scope so it's a way to limit the scope and capture closures. Does it have any practical use in c++?
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...
  }
Re:Under the hood of C++ lambdas and std::function by mfukar on Monday March 28, 2016 @09:23AM (#11374082)
Of course; with generic lambdas it's absolutely possible to code in a functional style: https://gist.github.com/mfukar/3214a140a5a3b35c59fa

That includes all the JS pass-a-lambda-as-a-callback shenanigans.

Re:Under the hood of C++ lambdas and std::function by catnaroek on Monday March 28, 2016 @04:18AM (#11373225)
No, you don't need this in C++. You can use a normal (brace-delimited) block for this purpose.
Re:Under the hood of C++ lambdas and std::function by evincarofautumn on Monday March 28, 2016 @06:35AM (#11373513)
The key difference being that a lambda is an expression, while a block is not, unless you use GCC’s statement-expression syntax of “({…})”.
Re:Under the hood of C++ lambdas and std::function by catnaroek on Monday March 28, 2016 @01:38PM (#11375799)
Oh, I forgot that, you're absolutely right. I'm getting too used to languages where everything is an expression. :-)
Re:Under the hood of C++ lambdas and std::function by BudVVeezer on Monday March 28, 2016 @12:01PM (#11375032)
You are forgetting attributes, which can be empty and are grammatical sequences (so you can add them infinitely).

[[]][[]][]()[[]][[]]{}();