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;








8















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;
();









share|improve this question
























  • 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

















8















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;
();









share|improve this question
























  • 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













8












8








8








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;
();









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









Saliya EkanayakeSaliya Ekanayake

1248 bronze badges




1248 bronze badges















  • 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

















  • 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
















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












2 Answers
2






active

oldest

votes


















16















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.






share|improve this answer

























  • 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


















6















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.






share|improve this answer





























    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
    );



    );













    draft saved

    draft discarded


















    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









    16















    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.






    share|improve this answer

























    • 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















    16















    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.






    share|improve this answer

























    • 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













    16














    16










    16









    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    6















    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.






    share|improve this answer































      6















      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.






      share|improve this answer





























        6














        6










        6









        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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






























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її