Namespaces
Variants
Views
Actions

Lambda functions

From cppreference.com
< cpp‎ | language
Revision as of 15:27, 30 January 2012 by Bazzy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:cpp/language/sidebar

Declares an unnamed function object

Contents

Syntax

[ Template:sparam ] ( Template:sparam ) -> Template:sparam (C++11)
[ Template:sparam ] ( Template:sparam ) (C++11)

Explanation

capture - specifies which symbols visible in the scope where the function is declared will be visible inside the function body.

A list of symbols can be passed as follows:

  • [a,&b] where ''a'' is captured by value and ''b'' is captured by reference.
  • [&] captures all symbols by reference
  • [=] captures all by value
  • [] captures nothing
params - The list of parameters, as in named functions
ret - Return type. If not present it's implied by the function return statements ( or void if it doesn't return any value)

Example 1

This example shows how to pass a lambda to a generic algorithm Template:example cpp

Example 2

Objects resulting from a lambda declaration, can be stored in std::function objects Template:example cpp

See also

(C++11)
copyable wrapper of any copy constructible callable object
(class template) [edit]