Skip to content

Commit f44f4e7

Browse files
author
mikeblome
committed
updates for 996966 move operations
1 parent 2324648 commit f44f4e7

3 files changed

Lines changed: 72 additions & 65 deletions

File tree

‎docs/cpp/assignment-operators.md‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
title: "Assignment Operators | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "11/04/2016"
4+
ms.date: "03/05/2018"
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology: ["cpp-language"]
88
ms.tgt_pltfrm: ""
99
ms.topic: "language-reference"
10-
f1_keywords: [">>=", "xor_eq", "&=", "<<=", "-=", "and_eq", "^=", "|=", "/=", "%=", "or_eq", "+=", "*="]
10+
f1_keywords: ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", "&&="]
1111
dev_langs: ["C++"]
12-
helpviewer_keywords: ["or_eq operator", "&= operator", "operators [C++], assignment", "assignment operators [C++], C++", "xor_eq operator", "+= operator", "and_eq operator", ">>= operator", "|= operator", "operator>>=", "*= operator", "%= operator", "^= operator", "operator >>=", "= operator", "assignment operators [C++]", "-= operator", "/= operator", "<<= operator"]
12+
helpviewer_keywords: ["operators [C++], assignment", "assignment operators [C++], C++", "&= operator", "&&= operator", "^= operator", "+= operator", ">>= operator", "|= operator", "operator>>=", "*= operator", "%= operator", "^= operator", "operator >>=", "= operator", "-= operator", "/= operator", "<<= operator"]
1313
ms.assetid: b028cf35-2ff1-4f14-9027-fd53ebec8aa0
1414
caps.latest.revision: 8
1515
author: "mikeblome"
@@ -21,14 +21,19 @@ ms.workload: ["cplusplus"]
2121
## Syntax
2222

2323
```
24-
25-
expression assignment-operator expression 
24+
expression assignment-operator expression 
2625
assignment-operator : one of
27-
   =   *=   /=   %=   +=   -=   <<=   >>=   &=   ^=   |=
26+
   =   *=   /=   %=   +=   -=   <<=   >>=   &=   ^=   |= &&=
2827
```
2928

3029
## Remarks
31-
Assignment operators store a value in the object designated by the left operand. There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand, and compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result. All assignment operators in the following table except the = operator are compound assignment operators.
30+
Assignment operators store a value in the object designated by the left operand. There are three kinds of assignment operations:
31+
32+
1. simple assignment, in which the value of the second operand is stored in the object specified by the first operand. 1. compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result.
33+
1. move assignment (for class types) in which resources are transferred without copying.
34+
35+
36+
All assignment operators in the following table except the = and &&= operators are compound assignment operators.
3237

3338
### Assignment Operators
3439

@@ -44,7 +49,8 @@ assignment-operator : one of
4449
|**>>=**|Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand.|
4550
|**&=**|Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand.|
4651
|`^=`|Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand.|
47-
|`&#124;=`|Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand.|
52+
|`&#124;=`|Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand.|
53+
|**&&=**| Move assignment operator (for class types only). If the second operand is an rvalue, move its resources to the first operand (without copying them). See [Move constructors and move assignment operators](move-constructors-and-move-assignment-operators-cpp.md) for more information.|
4854

4955
**Operator Keywords**
5056

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Value Categories: Lvalues and Rvalues (Visual C++) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "11/04/2016"
4+
ms.date: "03/05/2018"
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology: ["cpp-language"]
@@ -17,60 +17,61 @@ manager: "ghogen"
1717
ms.workload: ["cplusplus"]
1818
---
1919
# Lvalues and Rvalues (Visual C++)
20-
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.
20+
21+
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.
2122

2223
The C++17 standard defines expression value categories as follows:
2324

24-
- A *glvalue* is an expression whose evaluation determines the identity of an object, bit-field, or function.
25-
- A *prvalue* is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.
26-
- An *xvalue* is a glvalue that denotes an object or bit-field whose resources can be reused (usually because it is near the end of its lifetime). [ Example: Certain kinds of expressions involving rvalue references (8.3.2) yield xvalues, such as a call to a function whose return type is an rvalue reference or a cast to an rvalue reference type. ]
27-
- An *lvalue* is a glvalue that is not an xvalue.
28-
- An *rvalue* is a prvalue or an xvalue.
25+
- A *glvalue* is an expression whose evaluation determines the identity of an object, bit-field, or function.
26+
- A *prvalue* is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.
27+
- An *xvalue* is a glvalue that denotes an object or bit-field whose resources can be reused (usually because it is near the end of its lifetime). [ Example: Certain kinds of expressions involving rvalue references (8.3.2) yield xvalues, such as a call to a function whose return type is an rvalue reference or a cast to an rvalue reference type. ]
28+
- An *lvalue* is a glvalue that is not an xvalue.
29+
- An *rvalue* is a prvalue or an xvalue.
2930

3031
The following diagram illustrates the relationships between the categories:
3132

32-
![C++ expression value categories](media/value_categories.png "C++ expression value categories")
33-
34-
An lvalue has an address that your program can access. Examples of lvalue expressions include variable names, including `const` variables, array elements, function calls that return an lvalue reference, bit-fields, unions, and class members.
35-
36-
A prvalue expression has no address that is accessible by your program. Examples of prvalue expressions include literals, function calls that return a non-reference type, and temporary objects that are created during expression evalution but accessible only by the compiler.
37-
38-
An xvalue expression has no address but can be used to initialize an rvalue reference, which provides access to the expression. Examples include function calls that return an rvalue reference, and the array subscript, member and pointer to member expressions where the array or object is an rvalue reference.
39-
40-
The following example demonstrates several correct and incorrect usages of lvalues and rvalues:
41-
42-
```
43-
// lvalues_and_rvalues2.cpp
44-
int main()
45-
{
46-
int i, j, *p;
47-
48-
// Correct usage: the variable i is an lvalue and the literal 7 is a prvalue.
49-
i = 7;
50-
51-
// Incorrect usage: The left operand must be an lvalue (C2106). `j * 4` is a prvalue.
52-
7 = i; // C2106
53-
j * 4 = 7; // C2106
54-
55-
// Correct usage: the dereferenced pointer is an lvalue.
56-
*p = i;
57-
58-
const int ci = 7;
59-
// Incorrect usage: the variable is a non-modifiable lvalue (C3892).
60-
ci = 9; // C3892
61-
62-
// Correct usage: the conditional operator returns an lvalue.
63-
((i < 3) ? i : j) = 7;
64-
}
65-
```
66-
33+
![C++ expression value categories](media/value_categories.png "C++ expression value categories")
34+
35+
An lvalue has an address that your program can access. Examples of lvalue expressions include variable names, including `const` variables, array elements, function calls that return an lvalue reference, bit-fields, unions, and class members.
36+
37+
A prvalue expression has no address that is accessible by your program. Examples of prvalue expressions include literals, function calls that return a non-reference type, and temporary objects that are created during expression evalution but accessible only by the compiler.
38+
39+
An xvalue expression has an address that no longer accessible by your program but can be used to initialize an rvalue reference, which provides access to the expression. Examples include function calls that return an rvalue reference, and the array subscript, member and pointer to member expressions where the array or object is an rvalue reference.
40+
41+
The following example demonstrates several correct and incorrect usages of lvalues and rvalues:
42+
43+
```
44+
// lvalues_and_rvalues2.cpp
45+
int main()
46+
{
47+
int i, j, *p;
48+
49+
// Correct usage: the variable i is an lvalue and the literal 7 is a prvalue.
50+
i = 7;
51+
52+
// Incorrect usage: The left operand must be an lvalue (C2106).`j * 4` is a prvalue.
53+
7 = i; // C2106
54+
j * 4 = 7; // C2106
55+
56+
// Correct usage: the dereferenced pointer is an lvalue.
57+
*p = i;
58+
59+
const int ci = 7;
60+
// Incorrect usage: the variable is a non-modifiable lvalue (C3892).
61+
ci = 9; // C3892
62+
63+
// Correct usage: the conditional operator returns an lvalue.
64+
((i < 3) ? i : j) = 7;
65+
}
66+
```
67+
6768
> [!NOTE]
68-
> 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.
69-
70-
71-
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).
72-
73-
## See Also
74-
[Basic Concepts](../cpp/basic-concepts-cpp.md)
75-
[Lvalue Reference Declarator: &](../cpp/lvalue-reference-declarator-amp.md)
69+
> 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.
70+
71+
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).
72+
73+
## See Also
74+
75+
[Basic Concepts](../cpp/basic-concepts-cpp.md)
76+
[Lvalue Reference Declarator: &](../cpp/lvalue-reference-declarator-amp.md)
7677
[Rvalue Reference Declarator: &&](../cpp/rvalue-reference-declarator-amp-amp.md)

‎docs/cpp/move-constructors-and-move-assignment-operators-cpp.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
2-
title: "Move Constructors and Move Assignment Operators (C++) | Microsoft Docs"
2+
title: "How to: Define move constructors and move assignment operators (C++) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "11/04/2016"
4+
ms.date: "03/05/2018"
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology: ["cpp-language"]
88
ms.tgt_pltfrm: ""
99
ms.topic: "language-reference"
1010
dev_langs: ["C++"]
11-
helpviewer_keywords: ["move constructor"]
11+
helpviewer_keywords: ["move constructor [C++]"]
1212
ms.assetid: e75efe0e-4b74-47a9-96ed-4e83cfc4378d
1313
caps.latest.revision: 13
1414
author: "mikeblome"
@@ -17,7 +17,7 @@ manager: "ghogen"
1717
ms.workload: ["cplusplus"]
1818
---
1919
# Move Constructors and Move Assignment Operators (C++)
20-
This topic describes how to write a *move constructor* and a move assignment operator for a C++ class. A move constructor enables you to implement move semantics, which can significantly improve the performance of your applications. For more information about move semantics, see [Rvalue Reference Declarator: &&](../cpp/rvalue-reference-declarator-amp-amp.md).
20+
This topic describes how to write a *move constructor* and a move assignment operator for a C++ class. A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying. For more information about move semantics, see [Rvalue Reference Declarator: &&](../cpp/rvalue-reference-declarator-amp-amp.md).
2121

2222
This topic builds upon the following C++ class, `MemoryBlock`, which manages a memory buffer.
2323

@@ -126,7 +126,7 @@ private:
126126
```
127127
128128
### To create a move assignment operator for a C++ class
129-
129+
130130
1. Define an empty assignment operator that takes an rvalue reference to the class type as its parameter and returns a reference to the class type, as demonstrated in the following example:
131131
132132
```cpp
@@ -221,7 +221,7 @@ MemoryBlock& operator=(MemoryBlock&& other)
221221
```
222222

223223
## Example
224-
The following example shows how move semantics can improve the performance of your applications. The example adds two elements to a vector object and then inserts a new element between the two existing elements. In Visual C++ 2010, the `vector` class uses move semantics to perform the insertion operation efficiently by moving the elements of the vector instead of copying them.
224+
The following example shows how move semantics can improve the performance of your applications. The example adds two elements to a vector object and then inserts a new element between the two existing elements. The `vector` class uses move semantics to perform the insertion operation efficiently by moving the elements of the vector instead of copying them.
225225

226226
```cpp
227227
// rvalue-references-move-semantics.cpp
@@ -266,7 +266,7 @@ In ~MemoryBlock(). length = 50. Deleting resource.
266266
In ~MemoryBlock(). length = 75. Deleting resource.
267267
```
268268

269-
Before Visual C++ 2010, this example produces the following output:
269+
Before Visual Studio 2010, this example produced the following output:
270270

271271
```
272272
In MemoryBlock(size_t). length = 25.

0 commit comments

Comments
 (0)