What's the purpose of this lambda?What are the differences between a pointer variable and a reference variable in C++?How come a non-const reference cannot bind to a temporary object?Defining static const integer members in class definitionerror: passing xxx as 'this' argument of xxx discards qualifiersC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What is a lambda expression in C++11?What's the purpose of using braces (i.e. ) for a single-line if or loop?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsLambda returning itself: is this legal?
'Horseshoes' for Deer?
Can authors email you PDFs of their textbook for free?
Lob Logical Read and lob read-ahead reads in NCCI
Stock Volatility with Uncertain Probability
How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?
What is this "opened" cube called?
Rapid change in character
Find the logic in first 2 statements to give the answer for the third statement
Where should I draw the line on follow up questions from previous employer
Is it possible for a person to be tricked into becoming a lich?
Eliminate key lookup in execution plan
What's the difference between a variable and a memory location?
Can two aircraft be allowed to stay on the same runway at the same time?
Am I required to correct my opponent's assumptions about my morph creatures?
Cheap oscilloscope showing 16 MHz square wave
Unexpected behavior after assignment of function object to function wrapper
Why do IR remotes influence AM radios?
Coupling two 15 Amp circuit breaker for 20 Amp
My colleague treats me like he's my boss, yet we're on the same level
What's the purpose of this lambda?
What is a "hashed transaction" in SQL Server Replication terminology?
How to understand payment due date for credit card?
Why does Sauron not permit his followers to use his name?
IList<T> implementation
What's the purpose of this lambda?
What are the differences between a pointer variable and a reference variable in C++?How come a non-const reference cannot bind to a temporary object?Defining static const integer members in class definitionerror: passing xxx as 'this' argument of xxx discards qualifiersC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What is a lambda expression in C++11?What's the purpose of using braces (i.e. ) for a single-line if or loop?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsLambda returning itself: is this legal?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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;
();
c++ c++14
add a comment |
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;
();
c++ c++14
Call those functions beforemain
.
– Jarod42
8 hours ago
2
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning ofmain
.
– Brian
8 hours ago
2
@Brian: Unless of course, you're writing a library that has no control overmain
.
– Nicol Bolas
8 hours ago
add a comment |
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;
();
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;
();
c++ c++14
c++ c++14
asked 8 hours ago
Saliya EkanayakeSaliya Ekanayake
1248 bronze badges
1248 bronze badges
Call those functions beforemain
.
– Jarod42
8 hours ago
2
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning ofmain
.
– Brian
8 hours ago
2
@Brian: Unless of course, you're writing a library that has no control overmain
.
– Nicol Bolas
8 hours ago
add a comment |
Call those functions beforemain
.
– Jarod42
8 hours ago
2
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning ofmain
.
– Brian
8 hours ago
2
@Brian: Unless of course, you're writing a library that has no control overmain
.
– Nicol Bolas
8 hours ago
Call those functions before
main
.– Jarod42
8 hours ago
Call those functions before
main
.– Jarod42
8 hours ago
2
2
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning of
main
.– Brian
8 hours ago
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning of
main
.– Brian
8 hours ago
2
2
@Brian: Unless of course, you're writing a library that has no control over
main
.– Nicol Bolas
8 hours ago
@Brian: Unless of course, you're writing a library that has no control over
main
.– Nicol Bolas
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
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.
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
add a comment |
You typically see this code in competitive programming contest submissions for online judges that use C++ I/O behind the scenes. In order to boost the clock runtime of your submissions, you can untie the streams and unsychronize C++ IOStreams from the standard C streams. The use of the lambda is a slick way to execute the code immediately where the variable is declared. In order to get the full effect of this optimization it should be placed before any other code is executed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57733305%2fwhats-the-purpose-of-this-lambda%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
add a comment |
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.
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
add a comment |
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.
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.
answered 8 hours ago
BrianBrian
72k7 gold badges104 silver badges203 bronze badges
72k7 gold badges104 silver badges203 bronze badges
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
add a comment |
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
But the lambda isn't really what allows us to run arbitrary code right? We could use a normal function too? It seems to me the lambda is mainly there to make it a simple, single declaration?
– Bas in het Veld
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
@BasinhetVeld Yes, I agree.
– Brian
8 hours ago
add a comment |
You typically see this code in competitive programming contest submissions for online judges that use C++ I/O behind the scenes. In order to boost the clock runtime of your submissions, you can untie the streams and unsychronize C++ IOStreams from the standard C streams. The use of the lambda is a slick way to execute the code immediately where the variable is declared. In order to get the full effect of this optimization it should be placed before any other code is executed.
add a comment |
You typically see this code in competitive programming contest submissions for online judges that use C++ I/O behind the scenes. In order to boost the clock runtime of your submissions, you can untie the streams and unsychronize C++ IOStreams from the standard C streams. The use of the lambda is a slick way to execute the code immediately where the variable is declared. In order to get the full effect of this optimization it should be placed before any other code is executed.
add a comment |
You typically see this code in competitive programming contest submissions for online judges that use C++ I/O behind the scenes. In order to boost the clock runtime of your submissions, you can untie the streams and unsychronize C++ IOStreams from the standard C streams. The use of the lambda is a slick way to execute the code immediately where the variable is declared. In order to get the full effect of this optimization it should be placed before any other code is executed.
You typically see this code in competitive programming contest submissions for online judges that use C++ I/O behind the scenes. In order to boost the clock runtime of your submissions, you can untie the streams and unsychronize C++ IOStreams from the standard C streams. The use of the lambda is a slick way to execute the code immediately where the variable is declared. In order to get the full effect of this optimization it should be placed before any other code is executed.
edited 6 hours ago
answered 7 hours ago
0x499602D20x499602D2
71.5k29 gold badges125 silver badges213 bronze badges
71.5k29 gold badges125 silver badges213 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57733305%2fwhats-the-purpose-of-this-lambda%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Call those functions before
main
.– Jarod42
8 hours ago
2
@Jarod42 I think having this at namespace scope is fraught with peril. You could not guarantee whether it runs before or after any code that is part of initialization of non-local static variables in any other translation unit. And presumably this is what you care about - otherwise you would just put this at the beginning of
main
.– Brian
8 hours ago
2
@Brian: Unless of course, you're writing a library that has no control over
main
.– Nicol Bolas
8 hours ago