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
{{ message }}
Repository navigation
Commit f44f4e7
Browse filesBrowse the repository at this point in the historyBrowse files
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.
32
37
33
38
### Assignment Operators
34
39
@@ -44,7 +49,8 @@ assignment-operator : one of
44
49
|**>>=**|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.|
45
50
|**&=**|Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand.|
46
51
|`^=`|Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand.|
47
-
|`|=`|Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand.|
52
+
|`|=`|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.|
title: "Value Categories: Lvalues and Rvalues (Visual C++) | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/05/2018"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology: ["cpp-language"]
@@ -17,60 +17,61 @@ manager: "ghogen"
17
17
ms.workload: ["cplusplus"]
18
18
---
19
19
# 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.
21
22
22
23
The C++17 standard defines expression value categories as follows:
23
24
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.
29
30
30
31
The following diagram illustrates the relationships between the categories:
31
32
32
-

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
+

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
+
67
68
> [!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).
> 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).
Copy file name to clipboardExpand all lines: docs/cpp/move-constructors-and-move-assignment-operators-cpp.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
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"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/05/2018"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology: ["cpp-language"]
8
8
ms.tgt_pltfrm: ""
9
9
ms.topic: "language-reference"
10
10
dev_langs: ["C++"]
11
-
helpviewer_keywords: ["move constructor"]
11
+
helpviewer_keywords: ["move constructor [C++]"]
12
12
ms.assetid: e75efe0e-4b74-47a9-96ed-4e83cfc4378d
13
13
caps.latest.revision: 13
14
14
author: "mikeblome"
@@ -17,7 +17,7 @@ manager: "ghogen"
17
17
ms.workload: ["cplusplus"]
18
18
---
19
19
# 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).
21
21
22
22
This topic builds upon the following C++ class, `MemoryBlock`, which manages a memory buffer.
23
23
@@ -126,7 +126,7 @@ private:
126
126
```
127
127
128
128
### To create a move assignment operator for a C++ class
129
-
129
+
130
130
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:
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.
0 commit comments