Assignment operators
Assignment operators modify the value of the object.
Operator name  Syntax  Overloadable  Prototype examples (for class T)  

Inside class definition  Outside class definition  
simple assignment  a = b

Yes  T& T::operator =(const T2& b);  N/A 
addition assignment  a += b

Yes  T& T::operator +=(const T2& b);  T& operator +=(T& a, const T2& b); 
subtraction assignment  a = b

Yes  T& T::operator =(const T2& b);  T& operator =(T& a, const T2& b); 
multiplication assignment  a *= b

Yes  T& T::operator *=(const T2& b);  T& operator *=(T& a, const T2& b); 
division assignment  a /= b

Yes  T& T::operator /=(const T2& b);  T& operator /=(T& a, const T2& b); 
remainder assignment  a %= b

Yes  T& T::operator %=(const T2& b);  T& operator %=(T& a, const T2& b); 
bitwise AND assignment  a &= b

Yes  T& T::operator &=(const T2& b);  T& operator &=(T& a, const T2& b); 
bitwise OR assignment  a = b

Yes  T& T::operator =(const T2& b);  T& operator =(T& a, const T2& b); 
bitwise XOR assignment  a ^= b

Yes  T& T::operator ^=(const T2& b);  T& operator ^=(T& a, const T2& b); 
bitwise left shift assignment  a <<= b

Yes  T& T::operator <<=(const T2& b);  T& operator <<=(T& a, const T2& b); 
bitwise right shift assignment  a >>= b

Yes  T& T::operator >>=(const T2& b);  T& operator >>=(T& a, const T2& b); 

Contents 
[edit] Definitions
Copy assignment replaces the contents of the object a with a copy of the contents of b (b is not modified). For class types, this is performed in a special member function, described in copy assignment operator.
Move assignment replaces the contents of the object a with the contents of b, avoiding copying if possible (b may be modified). For class types, this is performed in a special member function, described in move assignment operator. 
(since C++11) 
For nonclass types, copy and move assignment are indistinguishable and are referred to as direct assignment.
Compound assignment replace the contents of the object a with the result of a binary operation between the previous value of a and the value of b.
[edit] Assignment operator syntax
The assignment expressions have the form
targetexpr = newvalue

(1)  
targetexpr op newvalue  (2)  
targetexpr    the expression^{[1]} to be assigned to 
op    one of *=, /= %=, += =, <<=, >>=, &=, ^=, = 
newvalue    the expression^{[2]}(until C++11)initializer clause(since C++11) to assign to the target 
 ↑ targetexpr must have higher precedence than an assignment expression.
 ↑ newvalue cannot be a comma expression, because its precedence is lower.
If newvalue is not an expression, the assignment expression will never match an overloaded compound assignment operator. 
(since C++11) 
[edit] Builtin simple assignment operator
For the builtin simple assignment, the object referred to by targetexpr is modified by replacing its value with the result of newvalue. targetexpr must be a modifiable lvalue.
The result of a builtin simple assignment is an lvalue of the type of targetexpr, referring to targetexpr. If targetexpr is a bitfield, the result is also a bitfield.
[edit] Assignment from an expression
If newvalue is an expression, it is implicitly converted to the cvunqualified type of targetexpr. When targetexpr is a bitfield that cannot represent the value of the expression, the resulting value of the bitfield is implementationdefined.
If targetexpr and newvalue identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same).
If the type of targetexpr is volatilequalified, the assignment is deprecated, unless the (possibly parenthesized) assignment expression is a discardedvalue expression or an unevaluated operand. 
(since C++20) 
Assignment from a nonexpression initializer clausenewvalue is only allowed not to be an expression in following situations:
#include <complex> std::complex<double> z; z = {1, 2}; // meaning z.operator=({1, 2}) z += {1, 2}; // meaning z.operator+=({1, 2}) int a, b; a = b = {1}; // meaning a = b = 1; a = {1} = b; // syntax error 
(since C++11) 
In overload resolution against userdefined operators, for every type T
, the following function signatures participate in overload resolution:
T*& operator=(T*&, T*); 

T*volatile & operator=(T*volatile &, T*); 

For every enumeration or pointer to member type T
, optionally volatilequalified, the following function signature participates in overload resolution:
T& operator=(T&, T); 

For every pair A1
and A2
, where A1
is an arithmetic type (optionally volatilequalified) and A2
is a promoted arithmetic type, the following function signature participates in overload resolution:
A1& operator=(A1&, A2); 

[edit] Builtin compound assignment operator
The behavior of every builtin compoundassignment expression targetexpr
op
=
newvalue is exactly the same as the behavior of the expression targetexpr
=
targetexpr
op
newvalue, except that targetexpr is evaluated only once.
The requirements on targetexpr and newvalue of builtin simple assignment operators also apply. Furthermore:
 For += and =, the type of targetexpr must be an arithmetic type or a pointer to a (possibly cvqualified) completelydefined object type.
 For all other compound assignment operators, the type of targetexpr must be an arithmetic type.
In overload resolution against userdefined operators, for every pair A1
and A2
, where A1
is an arithmetic type (optionally volatilequalified) and A2
is a promoted arithmetic type, the following function signatures participate in overload resolution:
A1& operator*=(A1&, A2); 

A1& operator/=(A1&, A2); 

A1& operator+=(A1&, A2); 

A1& operator=(A1&, A2); 

For every pair I1
and I2
, where I1
is an integral type (optionally volatilequalified) and I2
is a promoted integral type, the following function signatures participate in overload resolution:
I1& operator%=(I1&, I2); 

I1& operator<<=(I1&, I2); 

I1& operator>>=(I1&, I2); 

I1& operator&=(I1&, I2); 

I1& operator^=(I1&, I2); 

I1& operator=(I1&, I2); 

For every optionally cvqualified object type T
, the following function signatures participate in overload resolution:
T*& operator+=(T*&, std::ptrdiff_t); 

T*& operator=(T*&, std::ptrdiff_t); 

T*volatile & operator+=(T*volatile &, std::ptrdiff_t); 

T*volatile & operator=(T*volatile &, std::ptrdiff_t); 

[edit] Example
#include <iostream> int main() { int n = 0; // not an assignment n = 1; // direct assignment std::cout << n << ' '; n = {}; // zeroinitialization, then assignment std::cout << n << ' '; n = 'a'; // integral promotion, then assignment std::cout << n << ' '; n = {'b'}; // explicit cast, then assignment std::cout << n << ' '; n = 1.0; // floatingpoint conversion, then assignment std::cout << n << ' '; // n = {1.0}; // compiler error (narrowing conversion) int& r = n; // not an assignment r = 2; // assignment through reference std::cout << n << ' '; int* p; p = &n; // direct assignment p = nullptr; // nullpointer conversion, then assignment std::cout << p << ' '; struct { int a; std::string s; } obj; obj = {1, "abc"}; // assignment from a bracedinitlist std::cout << obj.a << ':' << obj.s << '\n'; }
Possible output:
1 0 97 98 1 2 (nil) 1:abc
[edit] Defect reports
The following behaviorchanging defect reports were applied retroactively to previously published C++ standards.
DR  Applied to  Behavior as published  Correct behavior 

CWG 1527  C++11  for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a userdefined assignment operator 
removed userdefined assignment constraint 
CWG 1538  C++11  E1 = {E2} was equivalent to E1 = T(E2) ( T is the type of E1 ), this introduced a Cstyle cast

it is equivalent to E1 = T{E2} 
CWG 2654  C++20  compound assignment operators for volatile qualified types were inconsistently deprecated 
none of them is deprecated 
CWG 2768  C++11  an assignment from a nonexpression initializer clause to a scalar value would perform directlistinitialization 
performs copylist initialization instead 
P2327R1  C++20  bitwise compound assignment operators for volatile types were deprecated while being useful for some platforms 
they are not deprecated 
[edit] See also
Common operators  

assignment  increment decrement 
arithmetic  logical  comparison  member access 
other 
a = b 
++a 
+a 
!a 
a == b 
a[...] 
function call 
a(...)  
comma  
a, b  
conditional  
a ? b : c  
Special operators  
static_cast converts one type to another related type 
C documentation for Assignment operators
