[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/lambda [Back]  [Original]

Lambda functions (depuis C++11) cppreference.com
cppreference.com
Espaces de noms
Variantes

Lambda functions (depuis C++11)

De cppreference.com


 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
Construit une fermeture: un objet fonction anonyme capable de capturer des variables dans la porte.
Original:
Constructs a fermeture: 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.

Syntaxe

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

1)

Dclaration complte
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)

Dclaration d'un lambda const: les objets capturs par copie ne peuvent pas tre modifis .
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)

Type de retour omis: le type de retour de la fermeture operator() est dduite d'aprs les rgles suivantes:
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.
  • si le body est compos de la mention return unique, le type de retour est le type de l'expression retourne (aprs conversion implicite rvalue--lvalue, tableau pointeur, ou fonction--pointeur)
    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.
  • autrement, le type de retour est 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)

Liste des paramtres omis: la fonction ne prend aucun argument, comme si la liste des paramtres est ()
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.

Explication

mutable - Permet
body de modifier les paramtres capturs par copie, et d'appeler leurs fonctions de membre non-const
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 -
fournit la spcification d'exception ou la noexcept clause pour l'oprateur () du type de fermeture
Original:
provides the spcification d'exception or the noexcept clause 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 -
fournit l'oprateur la spcification d'attribut () du type de fermeture
Original:
provides the spcification d'attribut 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.
capture -
spcifie les symboles visibles dans le champ o la fonction est dclare sera visible l'intrieur du corps de la fonction.
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Une liste de symboles peut tre adopte comme suit:

  • [a,&b] o a est captur par valeur et b est captur par rfrence.
  • [this] capte le pointeur this
  • [&] captures all symbols by reference
  • [=] captures all by value
  • [] captures nothing
params -
La liste des paramtres, comme dans nomme fonctions
Original:
The list of parameters, as in nomme fonctions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret -
Type de retour. S'il n'est pas prsent il est implicite dans les noncs de retour de fonction (ou void si elle ne retourne aucune valeur)
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 -
Corps de la fonction
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'expression lambda construit un objet non identifi temporaire unique, anonyme non syndiqu non agrge type, connu sous le nom' type de fermeture, ce qui comprend les membres suivants:
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()

<tbody> </tbody>
ret operator()(params) const { body }
(
le mot-cl mutable n'a pas t utilis
Original:
the keyword mutable was not used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
)
ret operator()(params) { body }
(
le mot-cl mutable a t utilis
Original:
the keyword mutable was used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
)

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)

<tbody> </tbody>
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()

<tbody> </tbody>
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 copier des constructeurs and dplacer des constructeurs.

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=()

<tbody> </tbody>
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()

<tbody> </tbody>
~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

<tbody> </tbody>
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.

Exemple

Cet exemple montre comment passer un lambda un algorithme gnrique et que les objets rsultant d'une dclaration lambda, peuvent tre stocks dans des objets 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'; 
}

Rsultat :

c: 5 6 7
func: 10

Voir aussi

auto spcificateur
spcifie un type dfini par l'expression (C++11)
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)
adaptateur gnrique de foncteur
(classe gnrique) [edit]

Web Proxy Viewer  |  New URL  |  Original Page