3

I saw the code from several solutions from LeetCode's problems.

static char __ = []() -> char {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
return '\0'; }();

I understood that the following lines are for fast I/O.

std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);

However, I have no idea about

static char __ = []() -> char {
    // fast IO
return '\0'; }();

Can someone explain what is meaning of the code?

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • 2
    Does this answer your question? [What is a lambda expression in C++11?](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – Brian Jun 10 '20 at 00:18
  • There might be better or more in depth targets out there, that was just the first from google. – NathanOliver Jun 10 '20 at 00:19
  • 3
    `__` identifier is reserved to the language implementation. If this variable definition is somewhere other than for example the standard library implementation, then it will have undefined behaviour. – eerorika Jun 10 '20 at 01:50
  • 1
    reopened, I think there is more to be explained about this code than just "what is a lambda" – M.M Jun 10 '20 at 01:58
  • BTW, you shouldn't use LeetCode as a place to learn C++. Or anything, really. The code there isn't necessarily good. – Nicol Bolas Jun 10 '20 at 02:18

3 Answers3

3

[]() -> char { ... } is a lambda taking no inputs and returning a char.

The () following the lambda is calling it, like any other function call.

The char value that the lambda is returning is being assigned to a static variable named __.

Since the variable is static, it will be initialized only once, so the lambda will be called only once.

See this answer to Is there a better way in c++ for one time execution of a set of code instead of using a static variable check

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
3

This code: []() -> char { return '\0'; }()

is an expression which calls an unnamed lambda function. In fact it contains some redundant elements and could have been written []{ return '\0'; }(). Since empty argument list in lambdas can be omitted, and the return type can be deduced from the expression in the return statement.

The code is very similar to:

char qux() { return '\0'; }
char __ = qux();

except that there is no need for the identifier qux and therefore reduced chance of collision with some other identifier the program uses.

Naming the variable __ causes undefined behaviour since names containing double-underscore are reserved to the implementation, it'd be better for this code to use some valid identifier and that can perhaps be hidden via an unnamed namespace if there is a collision.


The purpose of this code is to execute the function body before main() runs. It would be cleaner to have this code occur on the first line of main(), but perhaps the author was concerned that there might be some other code which also runs before main() and relies on these statements having executed.

However, this method does not really resolve the problem as if said other statements are in a different translation unit, there is no guarantee of __'s initialization occurring before the other unit's static initialization. See static initialization order fiasco.

M.M
  • 130,300
  • 18
  • 171
  • 314
2

It's a C++ lambda, kind of an inline function (ref: https://en.cppreference.com/w/cpp/language/lambda)

It has no capture [] and no input argument () and returns char (-> char).

After define the lambda, then the code calls the lambda function. That is the last (); part. Otherwise it just defines the lambda, but not use it.

B.Z.
  • 188
  • 2
  • 8