Namensräume
Varianten
Aktionen

Lambda functions (seit C++11)

Aus cppreference.com
< cpp‎ | language


 
 
Sprache C++
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausführung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 

Definiert ein Closure: ein unbenanntes (anonymes) Funktions-Literal, welches Variablen über deren Erstellungskontext hinaus einfangen und "am Leben erhalten" kann.

Inhaltsverzeichnis

[Bearbeiten] Syntax

[ capture ] ( params ) mutable exception attribute -> ret { body } (1)
[ capture ] ( params ) -> ret { body } (2)
[ capture ] ( params ) { body } (3)
[ capture ] { body } (4)

1) Vollständige Deklaration

2) Deklaration eines konstanten Lambdas: die darin eingefangenen Objekte können nicht verändert werden.

3) Auslassung des Rückgabetyps: Der Rückgabetyp der Closure wird wie folgt bestimmt:

  • Wenn der body aus einer einzigen return-Anweisung besteht, ist der Rückgabetyp der Typ des return-Ausdrucks (nach impliziter rvalue-zu-lvalue-, array-zu-Zeiger- oder Funktion-zu-Zeiger-Konvertierung)
  • Ansonsten ist der Rückgabetyp void

4) Ausgelassene Parameter-Liste: Die Funktion nimmt keine Parameter entgegen, als ob die Parameterliste () lauten würde

[Bearbeiten] Erklärung

mutable - erlaubt dem body, die durch Kopieren eingefangenen Parameter zu modifizieren und ihre nicht konstanten Membermethoden aufzurufen
exception - stellt die Exception-Spezifikation oder noexcept-Klausel für den Closure-Typ bereit
attribute - stellt die Attributspezifikation für den Closure-Typ bereit
capture - spezifiziert, welche Symbole erfasst und für den Funktionskörper sichtbar gemacht werden.

Eine Liste der Symbole kann wie folgt übergeben werden:

  • [a,&b], a wird kopiert und b als Referenz erfasst.
  • [this] erfasst den this-Zeiger als Kopie.
  • [&] erfasst alle im Funktionskörper verwendeten Symbole als Referenzen.
  • [=] erfasst alle im Funktionskörper verwendeten Symbole als Kopien.
  • [] es wird nichts erfasst.


params - Die Liste der Parameter, wie in benannten Funktionen
ret - Der Rückgabetyp. Falls nicht vorhanden, wird er von den return-Statements der Funktion abgeleitet (oder, falls jene nichts zurückgibt, void)
body - Funktionskörper

Der Lambda-Ausdruck ist ein prvalue Ausdruck, dessen Wert (bis C++17) und generiert (seit C++17) ein unbenanntes temporäres Objekt vom Typ non-union non-aggregate class, auch genannt 'closure type', das im kleinsten Block-geltungsbereich, Klassen-geltungsbereich oder namespace-geltungsbereich deklariert ist das den Lambda-Ausdruck enthält. Der 'closure type' enthält die folgenden Elemente:

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator()

ret operator()(params) const { body }
(das Schlüsselwort mutable wurde nicht verwendet)
ret operator()(params) { body }
(das Schlüsselwort mutable verwendet wurde)

Executes the body of the lambda-expression, when invoked. When accessing a variable, accesses its captured copy (for the entities captured by copy), or the original object (for the entities captured by reference). Unless the keyword mutable was used in the lamda-expression, the objects that were captured by copy are non-modifiable from inside this operator().

Dangling references

If an entity is captured by reference, implicitly or explicitly, and the function call operator of the closure object is invoked after the entity's lifetime has ended, undefined behavior occurs. The C++ closures do not extend the lifetimes of the captured references.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator ret(*)(params)

typedef ret(*F)(params);
operator F() const;

This member function is only defined if the capture list of the lambda-expression is empty.

The value returned by this conversion function is a function pointer that, when invoked, has the same effect as invoking the closure object's function call operator directly.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ClosureType()

ClosureType() = delete;
ClosureType(const ClosureType& ) = default;
ClosureType(ClosureType&& ) = default;

Closure types are not DefaultConstructible. The copy constructor and the move constructor are implicitly-declared and may be implicitly-defined according to the usual rules for implicit kopieren Konstruktoren and bewegen Konstruktoren.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator=()

ClosureType& operator=(const ClosureType&) = delete;

Closure types are not CopyAssignable.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
~ClosureType()

~ClosureType() = default;

The destructor is implicitly-declared.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CapturedParam

T1 a;

T2 b;

...

If the lambda-expression captures anything by copy (either implicitly with capture clause [=] or explicitly with a capture that does not include the character &, e.g. [a, b, c]), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured.

The type of each data member is the type of the corresponding captured entity, except if the entity has reference type (in that case, references to functions are captured as-is, and references to objects are captured as copies of the referenced objects).

For the entities that are captured by reference (with the default capture [&] or when using the character &, e.g. [&a, &b, &c]), it is unspecified if additional data members are declared in the closure type.

[Bearbeiten] Beispiel

Dieses Beispiel zeigt, wie eine Lambda auf einen generischen Algorithmus und dass Objekte, die sich aus einem Lambda-Erklärung übergeben, können in std::function Gegenstände gelagert werden .
Original:
This example shows how to pass a lambda to a generic algorithm and that objects resulting from a lambda declaration, can be stored in std::function objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> c { 1,2,3,4,5,6,7 };
    int x = 5;
    c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
 
    std::cout << "c: ";
    for (auto i: c) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    std::function<int (int)> func = [](int i) { return i+4; };
    std::cout << "func: " << func(6) << '\n'; 
}

Output:

c: 5 6 7
func: 10

[Bearbeiten] Siehe auch

auto Spezifizierer
gibt einen Typ durch einen Ausdruck (C++11) definiert
Original:
specifies a type defined by an expression (C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]
(C++11)
wickelt aufrufbare Objekt eines beliebigen Typs mit dem angegebenen Funktion Call-Signatur
Original:
wraps callable object of any type with specified function call signature
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(Klassen-Template) [edit]