You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/lambda-expression-syntax.md
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,16 +74,15 @@ int main()
74
74
8 is even
75
75
9 is odd
76
76
There are 4 even numbers in the vector.
77
-
78
77
```
79
78
80
79
### Comments
81
80
In the example, the third argument to the **for_each** function is a lambda. The `[&evenCount]` part specifies the capture clause of the expression, `(int n)` specifies the parameter list, and remaining part specifies the body of the expression.
82
81
83
82
## Example 2: Using a Function Object
84
-
Sometimes a lambda would be too unwieldy to extend much further than the previous example. The next example uses a function object instead of a lambda, together with the **for_each** function, to produce the same results as Example 1. Both examples store the count of even numbers in a `vector` object. To maintain the state of the operation, the `FunctorClass` class stores the `m_evenCount` variable by reference as a member variable. To perform the operation, `FunctorClass` implements the function-call operator, `operator()`. The Visual C++ compiler generates code that is comparable in size and performance to the lambda code in Example 1. For a basic problem like the one in this article, the simpler lambda design is probably better than the function-object design. However, if you think that the functionality might require significant expansion in the future, then use a function object design so that code maintenance will be easier.
83
+
Sometimes a lambda would be too unwieldy to extend much further than the previous example. The next example uses a function object instead of a lambda, together with the **for_each** function, to produce the same results as Example 1. Both examples store the count of even numbers in a `vector` object. To maintain the state of the operation, the `FunctorClass` class stores the `m_evenCount` variable by reference as a member variable. To perform the operation, `FunctorClass` implements the function-call operator, **operator()**. The Visual C++ compiler generates code that is comparable in size and performance to the lambda code in Example 1. For a basic problem like the one in this article, the simpler lambda design is probably better than the function-object design. However, if you think that the functionality might require significant expansion in the future, then use a function object design so that code maintenance will be easier.
85
84
86
-
For more information about the `operator()`, see [Function Call](../cpp/function-call-cpp.md). For more information about the **for_each** function, see [for_each](../standard-library/algorithm-functions.md#for_each).
85
+
For more information about the **operator()**, see [Function Call](../cpp/function-call-cpp.md). For more information about the **for_each** function, see [for_each](../standard-library/algorithm-functions.md#for_each).
Copy file name to clipboardExpand all lines: docs/cpp/lambda-expressions-constexpr.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# constexpr Lambda Expressions in C++
15
-
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): A lambda expression may be declared as `constexpr` or used in a contant expression when the initialization of each data member that it captures or introduces is allowed within a constant expression.
15
+
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): A lambda expression may be declared as **constexpr** or used in a contant expression when the initialization of each data member that it captures or introduces is allowed within a constant expression.
16
16
17
17
```cpp
18
18
int y = 32;
@@ -26,9 +26,8 @@ ms.workload: ["cplusplus"]
26
26
{
27
27
return [n] { return n + 1; }();
28
28
}
29
-
30
29
```
31
-
A lambda is implicitly `constexpr` if its result satisfies the requirements of a `constexpr` function:
30
+
A lambda is implicitly **constexpr** if its result satisfies the requirements of a **constexpr** function:
32
31
```cpp
33
32
auto answer = [](int n)
34
33
{
@@ -37,7 +36,7 @@ A lambda is implicitly `constexpr` if its result satisfies the requirements of a
37
36
38
37
constexpr int response = answer(10);
39
38
```
40
-
If a lambda is implicitly or explicitly `constexpr`, and you convert it to a function pointer, the resulting function is also `constexpr`:
39
+
If a lambda is implicitly or explicitly **constexpr**, and you convert it to a function pointer, the resulting function is also **constexpr**:
41
40
42
41
```cpp
43
42
auto Increment = [](int n)
@@ -48,7 +47,7 @@ If a lambda is implicitly or explicitly `constexpr`, and you convert it to a fun
48
47
constexpr int(*inc)(int) = Increment;
49
48
```
50
49
51
-
## See Also
50
+
## See also
52
51
[C++ Language Reference](../cpp/cpp-language-reference.md)
53
52
[Function Objects in the C++ Standard Library](../standard-library/function-objects-in-the-stl.md)
To use lambda expressions in the body of a class method, pass the `this` pointer to the capture clause to provide access to the methods and data members of the enclosing class.
97
+
To use lambda expressions in the body of a class method, pass the **this** pointer to the capture clause to provide access to the methods and data members of the enclosing class.
99
98
100
99
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): The **this** pointer may be captured by value by specifying `*this` in the capture clause. Capture by value means that the entire *closure*, which is the anonymous function object that encapulates the lambda expression, is copied to every call site where the lambda is invoked. Capture by value is useful when the lambda will execute in parallel or asynchronous operations, especially on certain hardware architectures such as NUMA.
In **C++ 14**, if the parameter type is generic, you can use the auto keyword as the type specifier. This tells the compiler to create the function call operator as a template. Each instance of auto in a parameter list is equivalent to a distinct type parameter.
@@ -328,7 +326,6 @@ vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18
328
326
{
329
327
return [n] { return n + 1; }();
330
328
}
331
-
332
329
```
333
330
A lambda is implicitly `constexpr` if its result satisfies the requirements of a `constexpr` function:
334
331
```cpp
@@ -363,8 +360,8 @@ auto Sqr = [](int t) __declspec(code_seg("PagedMem")) -> int { return t*t; };
363
360
364
361
In addition to C++11 Standard lambda functionality, Visual Studio supports stateless lambdas, which are omni-convertible to function pointers that use arbitrary calling conventions.
365
362
366
-
## See Also
363
+
## See also
367
364
[C++ Language Reference](../cpp/cpp-language-reference.md)
368
365
[Function Objects in the C++ Standard Library](../standard-library/function-objects-in-the-stl.md)
Copy file name to clipboardExpand all lines: docs/cpp/left-shift-and-right-shift-operators-input-and-output.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,6 @@ int main() {
48
48
bitset<16> bitset3{short3};
49
49
cout << bitset3 << endl; // 0000000000010000
50
50
}
51
-
52
51
```
53
52
54
53
If you left-shift a signed number so that the sign bit is affected, the result is undefined. The following example shows what happens in Visual C++ when a 1 bit is left-shifted into the sign bit position.
@@ -212,6 +211,6 @@ int main() {
212
211
213
212
The value of `E1 >> E2` is `E1` right-shifted `E2` bit positions. If `E1` has an unsigned type or if `E1` has a signed type and a non-negative value, the value of the result is the integral part of the quotient of **E1/2**<sup>**E2**</sup>. If `E1` has a signed type and a negative value, the resulting value is implementation-defined.
214
213
215
-
## See Also
214
+
## See also
216
215
[Expressions with Binary Operators](../cpp/expressions-with-binary-operators.md)
217
216
[C++ Built-in Operators, Precedence and Associativity](../cpp/cpp-built-in-operators-precedence-and-associativity.md)
0 commit comments