Lambda functions (dal C++11)

Da cppreference.com.
< cpp‎ | language


 
 
Linguaggio C + +
Temi generali
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.
Controllo del flusso
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.
Dichiarazioni esecuzione condizionale
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.
Iterazione dichiarazioni
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.
Vai dichiarazioni
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.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche(deprecato)
noexcept specificatore(C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
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 specificatori
Durata di stoccaggio specificatori
constexpr specificatore(C++11)
specificatore auto(C++11)
alignas specificatore(C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
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.
Tipi
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
Tipo alias dichiarazione(C++11)
attributi(C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
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.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
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.
Funzioni membro speciali
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.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni(C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
Costruisce un chiusura: un oggetto funzione senza nome in grado di catturare le variabili in campo.
Original:
Constructs a chiusura: an unnamed function object capable of capturing variables in scope.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Indice

[modifica]Sintassi

[capture](params)mutableexceptionattribute->ret{body} (1)
[capture](params)->ret{body} (2)
[capture](params){body} (3)
[capture]{body} (4)
1)
Dichiarazione completa
Original:
Full declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Dichiarazione di un const lambda: gli oggetti catturati dalla copia non può essere modificato.
Original:
Declaration of a const lambda: the objects captured by copy cannot be modified.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Omesso trailing-return-type: il tipo restituito di operator() la chiusura è dedotto in base alle seguenti regole:
Original:
Omitted trailing-return-type: the return type of the closure's operator() is deduced according to the following rules:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • se il body consiste dichiarazione return singolo, il tipo restituito è il tipo dell'espressione restituito (dopo rvalue-to-lvalue, array a puntatore, o funzione a puntatore conversione implicita)
    Original:
    if the body consists of the single return statement, the return type is the type of the returned expression (after rvalue-to-lvalue, array-to-pointer, or function-to-pointer implicit conversion)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • in caso contrario, il tipo restituito è void
    Original:
    otherwise, the return type is void
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
4)
Elenco di parametri Omesso: funzione non ha argomenti, come se la lista dei parametri è stato ()
Original:
Omitted parameter list: function takes no arguments, as if the parameter list was ()
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica]Spiegazione

mutable -
body permette di modificare i parametri catturati dalla copia, e di chiamare i loro non-const funzioni membro
Original:
allows body to modify the parameters captured by copy, and to call their non-const member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
exception -
fornisce il un'eccezione specifica o noexcept clausola per operatore () del tipo di chiusura
Original:
provides the un'eccezione specifica or the noexcept clausola for operator() of the closure type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
attribute -
fornisce il attributo specifica per operatore () del tipo di chiusura
Original:
provides the attributo specifica for operator() of the closure type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

{{par | capture |{{tr| specifica quali simboli visibili nel campo di applicazione in cui viene dichiarata la funzione sarà visibile all'interno del corpo della funzione. Un elenco di simboli può essere passato come segue:

  • [a,&b] dove a è catturato da valore e b viene catturato da riferimento.
  • [this] cattura il [[cpp / lingua / questo| 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.
  • [this] captures the [[cpp/language/this}}|this pointer]]
  • [&] captures all symbols by reference
  • [=] captures all by value
  • [] captures nothing }}
params -
L'elenco dei parametri, come in denominato funzioni
Original:
The list of parameters, as in denominato funzioni
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret -
Tipo restituito. Se non è presente è implicito istruzioni return la funzione (o nulla se non restituisce alcun valore)
Original:
Return type. If not present it's implied by the function return statements ( or void if it doesn't return any value)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
body -
Funzione del corpo
Original:
Function body
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


L'espressione lambda costruisce un oggetto senza nome temporaneo di unico senza nome non-unione non-aggregati di tipo, noto come tipo di chiusura, che ha i seguenti membri:
Original:
The lambda expression constructs an unnamed temporary object of unique unnamed non-union non-aggregate type, known as closure type, which has the following members:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

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 }
(la parola chiave mutevole non è stato utilizzato)
ret operator()(params){ body }
(la parola chiave è stata utilizzata mutevole)

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 copiare costruttori and spostare costruttori.

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.

[modifica]Esempio

Questo esempio mostra come passare un lambda a un algoritmo generico e che gli oggetti risultanti da una dichiarazione lambda, possono essere memorizzati in oggetti std::function .
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

[modifica]Vedi anche

specificatore auto
specifica un tipo definito da un (C++11) espressione
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.
[modifica]
(C++11)
avvolge oggetto invocabile di qualsiasi tipo con la firma specificato chiamata di funzione
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.

(classe template)[modifica]
close