Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of this lambda? [duplicate]

Tags:

c++

c++14

I see the following lambda in C++ code. What's the purpose of it?

static const auto faster = [](){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return nullptr;
}();
like image 783
Saliya Ekanayake Avatar asked Aug 30 '19 21:08

Saliya Ekanayake


People also ask

How do you duplicate Lambda?

When editing a lambda function, you can go Actions > Export Function > Download deployment package. This downloads a . zip file of the function. This duplicates the code and npm modules etc...

Why is Lambda running multiple times?

Any event Executing Lambda several times is due to retry behavior of Lambda as specified in AWS document. Your code might raise an exception, time out, or run out of memory. The runtime executing your code might encounter an error and stop. You might run out concurrency and be throttled.

What is the purpose of Lambda AWS?

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an ecommerce website.

What are Lambda aliases used for?

The AWS::Lambda::Alias resource creates an alias for a Lambda function version. Use aliases to provide clients with a function identifier that you can update to invoke a different version. You can also map an alias to split invocation requests between two versions.


1 Answers

A local static variable is initialized at most once, by the first thread that executes its declaration. By using a lambda, we can take advantage of this fact to run arbitrary code at most once. The first time the declaration is reached, the thread that reaches it will execute the code in the lambda as part of initializing the variable. The variable's value is presumably not actually used, but the program will remember that the variable has been initialized, so the lambda will not be run a second time.

like image 133
Brian Bi Avatar answered Oct 07 '22 17:10

Brian Bi