C++ Reference ยท Operators
C++ assignment operators
Reference for C++ assignment operators: simple and compound assignment, chaining, narrowing, copy and move assignment, self-assignment and sequencing rules.
The assignment operator = stores a value in an existing object. The compound forms (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=) combine an operation with the assignment: a += b means a = a + b except that a is evaluated only once.
Assignment is an expression: it yields the left operand as an lvalue, so it can be chained (a = b = 0). For class types, = calls the copy assignment or move assignment operator, which is different from initialization (T x = y; calls a constructor).
int a = 4; a += 3; // 7 a <<= 1; // 14 int b = a = 1;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
lhs = rhs lhs op= rhs // op is + - * / % & | ^ << >> T& T::operator=(const T&); // copy assignment T& T::operator=(T&&) noexcept; // move assignment T& T::operator+=(const T&); // compound assignment (member)
| Name | Description |
|---|---|
lhs |
A modifiable lvalue. |
rhs |
A value convertible to the type of lhs. In C++11 and later, a braced list is allowed: x = {1, 2}. |
Returns: lhs as an lvalue. Overloaded versions should return T& (*this) so chaining and (a = b).f() behave as for built-in types.
Errors / undefined behavior: assigning to a const object or a reference-to-const does not compile. Reading an uninitialized variable on the right is undefined behavior. For an overloaded operator=, forgetting the self-assignment case can destroy the data you are about to copy.
Usage notes
=inif:if (x = 5)assigns and tests the result. Compilers warn (-Wparentheses); write==for comparison, or a separate statement.- Right to left: all assignment operators are right-associative:
a = b += cmeansa = (b += c). - Sequencing: since C++17 the right operand is evaluated before the left, so
v[i] = i++is well defined, but still confusing; avoid it. - Compound versus long form:
a += bevaluatesaonce, which matters for expressions with side effects such asv[next()] += 1. - Narrowing: plain
=and compound assignment convert silently (int x = 2.7gives 2). Brace initializationint x{2.7}is an error. Use braces at declaration to catch it. - Rule of five: if you define a destructor, copy constructor or copy assignment, think about the other special functions too. With
std::string,std::vectorand smart pointers as members, the defaults (= default) are usually right. - Copy-and-swap: a single
T& operator=(T other) { swap(*this, other); return *this; }handles copy, move and self-assignment at once. volatilecompound assignment: deprecated in C++20 forvolatileoperands.