Anonymous function: Difference between revisions

Content deleted Content added
C++ (since C++11): Make it clear that specifying the return type is optional. The provided syntax is still incomplete
m C++ (since C++11): Avoid erroneous syntax highlighting
Line 603:
[[C++11]] supports anonymous functions (technically [[function object]]s), called ''lambda expressions'',<ref>{{Cite web|title=Lambda expressions (since C++11) - cppreference.com|url=https://en.cppreference.com/w/cpp/language/lambda|access-date=2022-01-14|website=en.cppreference.com}}</ref> which have the form:
<syntaxhighlight lang="cpp">
[capture](parameters) trailing-return-typetrailing_return_type { function_body }
</syntaxhighlight>
where "trailing-return-typetrailing_return_type" is optional and is of the form "-> return-typereturn_type". If it is absent, the return type is deduced from <code>return</code> statements as if for a function with declared return type <code>auto</code>.
 
This is an example lambda expression:
Line 732:
Since [[C++14]], the function parameters of a lambda can be declared with <code>auto</code>. The resulting lambda is called a ''generic lambda'' and is essentially an anonymous function template since the rules for type deduction of the auto parameters are the rules of template argument deduction. As of [[C++20]], template parameters can also be declared explicitly with the following syntax:
<syntaxhighlight lang="cpp">
[capture] <tparameters> (parameters) trailing-return-typetrailing_return_type { function_body }
</syntaxhighlight>