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/examples-of-lambda-expressions.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -282,15 +282,26 @@ int main()
282
282
283
283
### Example
284
284
You can use lambda expressions in the body of a function. The lambda expression can access any function or data member that the enclosing function can access. You can explicitly or implicitly capture the `this` pointer to provide access to functions and data members of the enclosing class.
285
+
**Visual Studio 2017 version 15.3 and later**: Capture `this` by value (`[*this]`) when the lambda will be used in asynchronous or parallel operations where the code might execute after the original object goes out of scope.
285
286
286
287
You can use the `this` pointer explicitly in a function, as shown here:
287
288
288
289
```cpp
290
+
291
+
// capture "this" by reference
289
292
voidApplyScale(const vector<int>& v) const
290
293
{
291
294
for_each(v.begin(), v.end(),
292
295
[this](int n) { cout << n * _scale << endl; });
293
296
}
297
+
298
+
// capture "this" by value (Visual Studio 2017 version 15.3 and later)
299
+
void ApplyScale2(const vector<int>& v) const
300
+
{
301
+
for_each(v.begin(), v.end(),
302
+
[*this](int n) { cout << n * _scale << endl; });
303
+
}
304
+
294
305
```
295
306
296
307
You can also capture the `this` pointer implicitly:
Copy file name to clipboardExpand all lines: docs/cpp/if-else-statement-cpp.md
+36-3Lines changed: 36 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,7 @@ else // optional
47
47
...
48
48
}
49
49
50
+
// Visual Studio 2017 version 15.3 and later:
50
51
if ( initialization; expression )
51
52
{
52
53
statement1;
@@ -57,6 +58,18 @@ else // optional
57
58
statement2;
58
59
...
59
60
}
61
+
62
+
// Visual Studio 2017 version 15.3 and later:
63
+
if constexpr (expression)
64
+
{
65
+
statement1;
66
+
...
67
+
}
68
+
else // optional
69
+
{
70
+
statement2;
71
+
...
72
+
}
60
73
```
61
74
## Example
62
75
```
@@ -105,7 +118,7 @@ int main()
105
118
}
106
119
```
107
120
## if statement with an initializer
108
-
**Visual Studio 2017 version 15.3 and later:** An **if** statement may also contain an initialization expression, as shown in the following example:
121
+
**Visual Studio 2017 version 15.3 and later**: An **if** statement may also contain an expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-block.
109
122
110
123
```cpp
111
124
## Example
@@ -143,18 +156,38 @@ int main()
143
156
shared_flag = false;
144
157
}
145
158
159
+
146
160
string s{ "if" };
147
-
if (auto keywords = { "if", "for", "while" }; any_of(keywords.begin(), keywords.end(), [&s](const char* kw) { return s == kw; }))
161
+
if (auto keywords = { "if", "for", "while" }; any_of(keywords.begin(), keywords.end(), [&s](const char* kw) { return s == kw; }))
148
162
{
149
163
cout << "Error! Token must not be a keyword\n";
150
164
}
151
165
152
166
}
153
167
```
154
-
## Remarks
168
+
155
169
In all forms of the **if** statement, *expression*, which can have any value except a structure, is evaluated, including all side effects. Control passes from the **if** statement to the next statement in the program unless one of the *statement*s contains a [break](../cpp/break-statement-cpp.md), [continue](../cpp/continue-statement-cpp.md), or [goto](../cpp/goto-statement-cpp.md).
156
170
157
171
The **else** clause of an `if...else` statement is associated with the closest previous **if** statement in the same scope that does not have a corresponding **else** statement.
172
+
173
+
## constexpr if statements
174
+
**Visual Studio 2017 version 15.3 and later**: In function templates, you can use a **constexpr if** statement to make compile-time branching decisions without having to resort to multiple function overloads. For example, you can write a single function that handles parameter unpacking (no zero-parameter overload is needed):
Copy file name to clipboardExpand all lines: docs/cpp/lambda-expressions-in-cpp.md
+54-8Lines changed: 54 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Lambda Expressions in C++ | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "07/19/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -35,8 +35,13 @@ translation.priority.ht:
35
35
- "zh-tw"
36
36
---
37
37
# Lambda Expressions in C++
38
-
In C++11 and later, a lambda expression—often called a *lambda*—is a convenient way of defining an anonymous function object right at the location where it is invoked or passed as an argument to a function. Typically lambdas are used to encapsulate a few lines of code that are passed to algorithms or asynchronous methods. This article defines what lambdas are, compares them to other programming techniques, describes their advantages, and provides a basic example.
39
-
38
+
In C++11 and later, a lambda expression—often called a *lambda*—is a convenient way of defining an anonymous function object (a *closure*) right at the location where it is invoked or passed as an argument to a function. Typically lambdas are used to encapsulate a few lines of code that are passed to algorithms or asynchronous methods. This article defines what lambdas are, compares them to other programming techniques, describes their advantages, and provides a basic example.
39
+
40
+
## Related Topics
41
+
-[Lambda expressions vs. function objects](lambda-expression-syntax.md)
42
+
-[Working with lambda expressions](examples-of-lambda-expressions.md)
[&, &i]{}; // ERROR: i preceded by & when & is the default
100
-
[=, this]{}; // ERROR: this when = is the default
101
-
[i, i]{}; // ERROR: i repeated
103
+
[&, i]{}; // OK
104
+
[&, &i]{}; // ERROR: i preceded by & when & is the default
105
+
[=, this]{}; // ERROR: this when = is the default
106
+
[=, *this]{ }; // OK: captures this by value. See below.
107
+
[i, i]{}; // ERROR: i repeated
102
108
}
103
109
```
104
110
@@ -112,7 +118,10 @@ void f(Args... args) {
112
118
}
113
119
```
114
120
115
-
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. For an example that shows how to use lambda expressions with class methods, see "Example: Using a Lambda Expression in a Method" in [Examples of Lambda Expressions](../cpp/examples-of-lambda-expressions.md).
121
+
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.
122
+
**Visual Studio 2017 version 15.3 and later**: 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.
123
+
124
+
For an example that shows how to use lambda expressions with class methods, see "Example: Using a Lambda Expression in a Method" in [Examples of Lambda Expressions](../cpp/examples-of-lambda-expressions.md).
116
125
117
126
When you use the capture clause, we recommend that you keep these points in mind, particularly when you use lambdas with multithreading:
118
127
@@ -325,6 +334,43 @@ vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18
325
334
```
326
335
327
336
For more information, see [generate_n](../standard-library/algorithm-functions.md#generate_n).
337
+
338
+
## constexpr lambda expressions
339
+
**Visual Studio 2017 version 15.3 and later**: 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.
340
+
341
+
```cpp
342
+
int y = 32;
343
+
auto answer = [y]() constexpr
344
+
{
345
+
int x = 10;
346
+
return y + x;
347
+
};
348
+
349
+
constexpr int Increment(int n)
350
+
{
351
+
return [n] { return n + 1; }();
352
+
}
353
+
354
+
```
355
+
A lambda is implicitly `constexpr` if its result satisfies the requirements of a `constexpr` function:
356
+
```cpp
357
+
auto answer = [](int n)
358
+
{
359
+
return 32 + n;
360
+
};
361
+
362
+
constexpr int response = answer(10);
363
+
```
364
+
If a lambda is implicitly or explicitly `constexpr`, conversion to a function pointer produces a `constexpr` function:
365
+
366
+
```cpp
367
+
auto Increment = [](int n)
368
+
{
369
+
return n + 1;
370
+
};
371
+
372
+
constexpr int(*inc)(int) = Increment;
373
+
```
328
374
329
375
## Microsoft-Specific
330
376
Lambdas are not supported in the following common language runtime (CLR) managed entities: `ref class`, `ref struct`, `value class`, or `value struct`.
Copy file name to clipboardExpand all lines: docs/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,8 @@ f1_keywords:
13
13
- "#endif"
14
14
- "#if"
15
15
- "#elif"
16
-
- "Defined"
16
+
- "defined"
17
+
- "__has_include"
17
18
dev_langs:
18
19
- "C++"
19
20
helpviewer_keywords:
@@ -111,7 +112,8 @@ The `#if` directive, with the `#elif`, `#else`, and `#endif` directives, control
111
112
- The translator can translate character constants to a set of code values different from the set for the target environment. To determine the properties of the target environment, check values of macros from LIMITS.H in an application built for the target environment.
112
113
113
114
- The expression must not perform any environmental inquiries and must remain insulated from implementation details on the target computer.
114
-
115
+
116
+
## defined
115
117
The preprocessor operator **defined** can be used in special constant expressions, as shown by the following syntax:
116
118
117
119
defined( `identifier` )
@@ -190,6 +192,24 @@ class Example
190
192
```
191
193
192
194
The preceding code checks to see if the symbolic constant `EXAMPLE_H` is defined. If so, the file has already been included and need not be reprocessed. If not, the constant `EXAMPLE_H` is defined to mark EXAMPLE.H as already processed.
195
+
196
+
## __has_include
197
+
**Visual Studio 2017 version 15.3 and later**: Determines whether a library header is available for inclusion:
0 commit comments