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/attributes2.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,9 +40,9 @@ void Foo(int);
40
40
41
41
-`deprecated` - **Visual Studio 2015 and later:** Specifies that a function is not intended to be used, and might not exist in future versions of a library interface. The compiler can use this to generate an informational message when client code attempts to call the function. Can be applied to declaration of a class, a typedef-name, a variable, a non-static data member, a function, a namespace, an enumeration, an enumerator, or a template specialization.
42
42
43
-
-`[[fallthrough]]` - **Visual Studio 2017 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) The `[[fallthrough]]` attribute can be used in the context of [switch](switch-statement-cpp.md) statements as a hint to the compiler (or anyone reading the code) that the fallthrough behavior is intended. The Visual C++ compiler currently does not warn on fallthrough behavior, so this attribute has no effect compiler behavior.
43
+
-`[[fallthrough]]` - **Visual Studio 2017 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) The `[[fallthrough]]` attribute can be used in the context of [switch](switch-statement-cpp.md) statements as a hint to the compiler (or anyone reading the code) that the fallthrough behavior is intended. The Visual C++ compiler currently does not warn on fallthrough behavior, so this attribute has no effect compiler behavior.
44
44
45
-
-`[[nodiscard]]`**Visual Studio 2017 version 15.3 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) Specifies that a function's return value is not intended to be discarded. Raises warning C4834, as shown in the following example
45
+
-`[[nodiscard]]`**Visual Studio 2017 version 15.3 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) Specifies that a function's return value is not intended to be discarded. Raises warning C4834, as shown in the following example
46
46
47
47
```cpp
48
48
[[nodiscard]]
@@ -55,7 +55,7 @@ int main()
55
55
}
56
56
```
57
57
58
-
- `[[maybe_unused]]` **Visual Studio 2017 version 15.3 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) Specifies that a variable, function, class, typedef, non-static data member, enum, or template specialization may intentionally not be used. The compiler does not warn when an entity marked `[[maybe_unused]]` is not used. An entity that is declared without the attribute can later be redeclared with the attribute and vice versa. An entity is considered marked after its first declaration that is marked is analyzed, and for the remainder of translation of the current translation unit.
58
+
- `[[maybe_unused]]` **Visual Studio 2017 version 15.3 and later:**(available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) Specifies that a variable, function, class, typedef, non-static data member, enum, or template specialization may intentionally not be used. The compiler does not warn when an entity marked `[[maybe_unused]]` is not used. An entity that is declared without the attribute can later be redeclared with the attribute and vice versa. An entity is considered marked after its first declaration that is marked is analyzed, and for the remainder of translation of the current translation unit.
Copy file name to clipboardExpand all lines: docs/cpp/bool-cpp.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,8 @@ translation.priority.ht:
39
39
---
40
40
# bool (C++)
41
41
This keyword is a built-in type. A variable of this type can have values [true](../cpp/true-cpp.md) and [false](../cpp/false-cpp.md). Conditional expressions have the type `bool` and so have values of type `bool`. For example, `i!=0` now has **true** or **false** depending on the value of `i`.
42
+
43
+
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): The operand of a postfix or prefix increment or decrement operator may not be of type `bool`.
42
44
43
45
The values **true** and **false** have the following relationship:
44
46
@@ -55,8 +57,6 @@ if (condexpr1) statement1;
55
57
56
58
If `condexpr1` is **true**, `statement1` is always executed; if `condexpr1` is **false**, `statement1` is never executed.
57
59
58
-
When a postfix or prefix `++` operator is applied to a variable of type `bool`, the variable is set to **true**. The postfix or prefix `--` operator cannot be applied to a variable of this type.
59
-
60
60
The `bool` type participates in integral promotions. An r-value of type `bool` can be converted to an r-value of type `int`, with **false** becoming zero and **true** becoming one. As a distinct type, `bool` participates in overload resolution.
Notice that the line `hand = account_num;` still causes the error that occurs with unscoped enums, as shown earlier. It is allowed with an explicit cast. However, with scoped enums, the attempted conversion in the next statement, `account_num = Suit::Hearts;`, is no longer allowed without an explicit cast.
168
168
169
169
## Enums with no enumerators
170
-
**Visual Studio 2017 version 15.3 and later**:By defining an enum (regular or scoped) with an explicit underlying type and no enumerators, you can in effect introduce a new integral type that has no implicit conversion to any other type. By using this type instead of its built-in underlying type, you can eliminate the potential for subtle errors caused by inadvertent implicit conversions.
170
+
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): By defining an enum (regular or scoped) with an explicit underlying type and no enumerators, you can in effect introduce a new integral type that has no implicit conversion to any other type. By using this type instead of its built-in underlying type, you can eliminate the potential for subtle errors caused by inadvertent implicit conversions.
171
171
172
172
173
173
```cpp
@@ -185,8 +185,8 @@ E e2 = E{ 0 };
185
185
186
186
struct X
187
187
{
188
-
E e{ 0 };
189
-
X() : e{ 0 } { }
188
+
E e{ 0 };
189
+
X() : e{ 0 } { }
190
190
};
191
191
192
192
E* p = new E{ 0 };
@@ -195,12 +195,12 @@ void f(E e) {};
195
195
196
196
int main()
197
197
{
198
-
f(E{ 0 });
199
-
byte i{ 42 };
200
-
byte j = byte{ 42 };
198
+
f(E{ 0 });
199
+
byte i{ 42 };
200
+
byte j = byte{ 42 };
201
201
202
-
// unsigned char c = j; // C2440: 'initializing': cannot convert from 'byte' to 'unsigned char'
203
-
return 0;
202
+
// unsigned char c = j; // C2440: 'initializing': cannot convert from 'byte' to 'unsigned char'
3.**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): Use structured bindings The advantage of structured bindings is that the variables that store the return values are initialized at the same time they are declared, which in some cases can be significantly more efficient. In this statement --`auto[x, y, z] = f();`-- the brackets introduce and intialize names that are in scope for the entire function block.
346
+
3.**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): Use structured bindings. The advantage of structured bindings is that the variables that store the return values are initialized at the same time they are declared, which in some cases can be significantly more efficient. In this statement --`auto[x, y, z] = f();`-- the brackets introduce and intialize names that are in scope for the entire function block.
344
347
345
348
```cpp
346
349
#include<tuple>
@@ -351,36 +354,36 @@ using namespace std;
351
354
352
355
tuple<int, string, double> f()
353
356
{
354
-
int i{ 108 };
355
-
string s{ "Some text" };
356
-
double d{ .01 };
357
-
return { i,s,d };
357
+
int i{ 108 };
358
+
string s{ "Some text" };
359
+
double d{ .01 };
360
+
return { i,s,d };
358
361
}
359
362
structS
360
363
{
361
-
string name;
362
-
int num;
364
+
string name;
365
+
int num;
363
366
};
364
367
365
368
S g()
366
369
{
367
-
string t{ "hello" };
368
-
int u{ 42 };
369
-
return { t, u };
370
+
string t{ "hello" };
371
+
int u{ 42 };
372
+
return { t, u };
370
373
}
371
374
372
375
intmain()
373
376
{
374
-
auto[x, y, z] = f(); // init from tuple
375
-
cout << x << " " << y << " " << z << endl;
377
+
auto[x, y, z] = f(); // init from tuple
378
+
cout << x << " " << y << " " << z << endl;
376
379
377
-
auto[a, b] = g(); // init from POD struct
378
-
cout << a << " " << b << endl;
379
-
return 0;
380
+
auto[a, b] = g(); // init from POD struct
381
+
cout << a << " " << b << endl;
382
+
return 0;
380
383
}
381
384
```
382
385
383
-
4.Although not "returning values" in the strict sense, you can define any number of parameters to use pass-byreference so that the function can modify or initialize the values of objects that the caller provides. For more information, see [Reference-Type Function Arguments](reference-type-function-arguments.md).
386
+
4.In addition to using the return value itself, you can "return" values by defining any number of parameters to use pass-by-reference so that the function can modify or initialize the values of objects that the caller provides. For more information, see [Reference-Type Function Arguments](reference-type-function-arguments.md).
384
387
385
388
## Function pointers
386
389
C++ supports function pointers in the same manner as the C language. However a more type-safe alternative is usually to use a function object.
Copy file name to clipboardExpand all lines: docs/cpp/lambda-expressions-in-cpp.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,6 +119,7 @@ void f(Args... args) {
119
119
```
120
120
121
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
+
122
123
**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.
123
124
124
125
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).
@@ -336,7 +337,7 @@ vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18
336
337
For more information, see [generate_n](../standard-library/algorithm-functions.md#generate_n).
337
338
338
339
## constexpr lambda expressions
339
-
**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.
340
+
**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 constant expression when the initialization of each data member that it captures or introduces is allowed within a constant expression.
Copy file name to clipboardExpand all lines: docs/cpp/lvalues-and-rvalues-visual-cpp.md
+1-35Lines changed: 1 addition & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ translation.priority.ht:
34
34
- "zh-tw"
35
35
---
36
36
# Lvalues and Rvalues (Visual C++)
37
-
Every C++ expression has a type, and belongs to a *value category*. The value categories are the basis for rules that compilers must follow when creating, copying, and moving temporary objects during expression evaluation. In C++17 (**Visual Studio version 15.3 and later**) the rules were restated to ensure that all compilers behave identically by not creating objects unless they are actually required. The new specified behavior is called "guaranteed copy elision." It helps to make your code more portable and efficient and eliminates the need to provide copy and move constructors for types that never use them.
37
+
Every C++ expression has a type, and belongs to a *value category*. The value categories are the basis for rules that compilers must follow when creating, copying, and moving temporary objects during expression evaluation.
38
38
39
39
The C++17 standard defines expression value categories as follows:
40
40
@@ -84,40 +84,6 @@ int main()
84
84
> [!NOTE]
85
85
> The examples in this topic illustrate correct and incorrect usage when operators are not overloaded. By overloading operators, you can make an expression such as `j * 4` an lvalue.
86
86
87
-
The following example shows the new behavior for guaranteed copy elision. Note that construction from temporary objects succeeds in both cases despite the absence of a move constructor.
88
-
89
-
```cpp
90
-
#include<iostream>
91
-
#include<string>
92
-
93
-
usingnamespacestd;
94
-
95
-
structS {
96
-
S(int) { cout << "S(int)" << endl; }
97
-
S(S&) = delete;
98
-
S(S&&) = delete; // { cout << "move" << endl; }
99
-
///...
100
-
};
101
-
102
-
S make_s()
103
-
{
104
-
// Return value initialized directly at call site.
105
-
// In Visual Studio 2015 this does not compile due to deleted move ctor.
106
-
return S(42);
107
-
}
108
-
109
-
110
-
int main()
111
-
{
112
-
auto nm = make_s();
113
-
S x4 = 5; // Construct from an rvalue.
114
-
}
115
-
```
116
-
The program produces this output:
117
-
```output
118
-
S(int)
119
-
S(int)
120
-
```
121
87
122
88
The terms *lvalue* and *rvalue* are often used when you refer to object references. For more information about references, see [Lvalue Reference Declarator: &](../cpp/lvalue-reference-declarator-amp.md) and [Rvalue Reference Declarator: &&](../cpp/rvalue-reference-declarator-amp-amp.md).
0 commit comments