Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

When does C++ use lambda expressions

Shulou Source: shulou.com Published: 2022-06-01 19:19:05 11月04日 Update

This article mainly introduces "when does C++ use lambda expression". In daily operation, I believe many people have doubts about when C++ uses lambda expression. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "when does C++ use lambda expression?" Next, please follow the editor to study!

F.50: use lambda expressions when you don't want to use functions (such as reading local variables, accessing local functions)

Reason (reason)

Functions cannot use local variables outside the function body, nor can they be defined in the local scope; if you need this functionality, it is better to use lambda expressions if possible, otherwise you need to implement the function object yourself. On the other hand, lambda expressions and function objects cannot be overloaded; if you need to overload, functions are more appropriate (the method of overloading lambda expressions by twists and turns is too advanced). If both ways are available, it is better to use functions; use the simplest tool that meets your needs.

Example (sample)

/ / writing a function that should only take an int or a string//-- overloading is naturalvoid f (int); void f (const string&)

/ / writing a function object that needs to capture local state and appear// at statement or expression scope-- a lambda is naturalvector v = lots_of_work (); for (int tasknum = 0; tasknum < max; + + tasknum) {pool.run ([=, & v] {/ *. Process 1 / max-th of v, the tasknum-th chunk... * /});} pool.join (); Exception (exception)

Normal lambda expressions are useful because they provide a concise way to implement function templates; an ordinary function template requires even a slightly more complex syntax to do the same thing. But once all functions can have conceptual parameters in the future, this advantage is likely to disappear in the future.

Translator's note: Concept is a new feature that will be introduced by Category 20.

Enforcement (implementation recommendations)

Alarm when a named non-ordinary lambda expression that does not get any variables and exists in the global scope (such as auto x = [] (int) {/ *... * /};) is used.

At this point, the study of "when does C++ use lambda expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Functions expressions timing caching local learning variables ordinary actions objects ways methods more templates help appropriate complex practical same concise Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno macOS Apple NVidia