Anonymous function: Difference between revisions

Content deleted Content added
Ruby: fix syntaxhighlight error
C++ (since C++11): Add some more features of C++ lambdas from various versions of the language
Line 711:
func_ptr(4); //calls the lambda.
</syntaxhighlight>
 
Starting from [[C++23]], a lambda expression can be recursive through explicit <code>this</code> as first parameter:
Starting from [[C++17]], the lambda can be declared <code>constexpr</code> or <code>consteval</code> with the usual semantics. These specifiers go after the parameter list, like <code>mutable</code>. Starting from [[C++23]], the lambda can also be <code>static</code> if it has no captures. The <code>static</code> and <code>mutable</code> specifiers are not allowed to be combined and doing so would not be of any use anyway.
 
StartingAlso fromsince [[C++23]], a lambda expression can be recursive through explicit <code>this</code> as first parameter:
 
<syntaxhighlight lang="cpp">
Line 721 ⟶ 724:
<syntaxhighlight lang="cpp">
for_each(a.begin(), a.end(), std::cout << _1 << ' ');
</syntaxhighlight>
 
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) -> return_type { function_body }
</syntaxhighlight>