Lambda functions (C++11 起)
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
You can help to correct and verify the translation. Click here for instructions.
[编辑] 語法
[ capture ] ( params ) mutable exception attribute -> ret { body }
|
(1) | ||||||||
[ capture ] ( params ) -> ret { body }
|
(2) | ||||||||
[ capture ] ( params ) { body }
|
(3) | ||||||||
[ capture ] { body }
|
(4) | ||||||||
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
operator()的返回類型是根據以下規則推導出的operator() is deduced according to the following rules:You can help to correct and verify the translation. Click here for instructions.
- 如果body由單return表,返回類型是返回的表達式類型的(後右值到左值,數組與指針,函數指針的隱式轉換)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.
- 否則,返回類型是voidOriginal:otherwise, the return type is voidThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
()()You can help to correct and verify the translation. Click here for instructions.
[编辑] 解釋
| mutable | - | body修改捕獲的參數拷貝,並呼籲他們的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 | - | 提供異常規範或noexcept條款封閉類型的操作符()
Original: provides the 異常規範 or the noexcept條款 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 | - | 提供屬性說明封閉類型的操作符()
{{param list item | capture |Original: provides the 屬性說明 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. 指定的符號出現在函數被聲明的範圍內都可以看到裡面的函數體。
可以通過一個符號列表如下:
|
| params | - | 參數列表,在命名函數
Original: The list of parameters, as in 命名函數 The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| ret | - | 返回類型。如果不存在,它的暗示該函數的返回語句(或無效的,如果它不返回任何值)
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 | - | 函數體
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. |
You can help to correct and verify the translation. Click here for instructions.
ClosureTypeOriginal:ClosureType::The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.operator()
You can help to correct and verify the translation. Click here for instructions.
| ret operator()(params) const { body } |
(關鍵字mutable沒有使用) | |
| ret operator()(params) { body } |
(使用了可變的關鍵字) | |
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.
ClosureTypeOriginal: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)
You can help to correct and verify the translation. Click here for instructions.
| 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.
ClosureTypeOriginal:ClosureType::The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.ClosureType()
You can help to correct and verify the translation. Click here for instructions.
| 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 拷貝構造函數 and 移動的構造函數.
ClosureTypeOriginal:ClosureType::The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.operator=()
You can help to correct and verify the translation. Click here for instructions.
| ClosureType& operator=(const ClosureType&) = delete; |
||
Closure types are not CopyAssignable.
ClosureTypeOriginal:ClosureType::The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.~ClosureType()
You can help to correct and verify the translation. Click here for instructions.
| ~ClosureType() = default; |
||
The destructor is implicitly-declared.
ClosureTypeOriginal:ClosureType::The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.CapturedParam
You can help to correct and verify the translation. Click here for instructions.
| 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.
| 本節是不完整的 原因: scope rules, capture list rules, nested lambdas, implicit capture vs odr use, decltype |
[编辑] 為例
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
[编辑] 另請參閱
| 汽車符 | 指定表達(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. |
| (C++11) |
包裝任何類型的可調用對象與指定的函數調用簽名 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. (類模板) |