Assignment operators

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Assignment operators modify the value of the object.

Operator name Syntax Over​load​able Prototype examples (for class T)
Inside class definition Outside class definition
basic assignment a = b Yes T& T::operator =(const T2& b); N/A
move assignment (C++11) a = rvalue Yes T& T::operator =(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);
modulo 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);
Notes
  • All built-in assignment operators return *this, and most user-defined overloads also return *this so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
  • T2 can be any type including T

Contents

[edit] Explanation

copy assignment operator replaces the contents of the object a with a copy of the contents of b (b is not modified). For class types, this is a special member function, described in copy assignment operator.

move assignment operator 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 a special member function, described in move assignment operator. (since C++11)

For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment.

compound assignment operators replace the contents 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] Builtin direct assignment

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 volatile-qualified, 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 volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:

A1& operator=(A1&, A2);

For expressions E1 of any scalar type T, the following additional forms of the builtin assignment expression are allowed:

E1 = {}
(since C++11)
E1 = {E2}
(since C++11)

Note: the above includes all non-class types except reference types, array types, function types, and the type void, which are not directly assignable.

The direct assignment operator expects a modifiable lvalue as its left operand and returns an lvalue identifying the left operand after modification. For non-class types, the right operand is first implicitly converted to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.

When the left operand is a reference type, the assignment operator applies to the referred-to object.

If the left and the right operands identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same)

If the right operand is a braced-init-list

  • the expression E1 = {} is equivalent to E1 = T(), where T is the type of E1.
  • the expression E1 = {E2} is equivalent to E1 = T(E2), where T is the type of E1, except that narrowing implicit conversions are prohibited.

For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of list-initialization

[edit] Example

#include <iostream>
int main()
{
    int n = 0;  // not an assignment
    n = 1;      // direct asignment
    std::cout << n << ' ';
    n = {};     // zero-initialization, 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;    // floating-point conversion, then assignment
    std::cout << n << ' ';
//    n = {1.0}; // compiler error (narrowing conversion)
 
    int& r = n;  // not an assignment
    int* p;
 
    r = 2;       // assignment through reference
    std::cout << n << ' ';
    p = &n;      // direct assignment
    p = nullptr; // null-pointer conversion, then assignment 
}

Output:

1 0 97 98 1 2

[edit] Builtin compound assignment

For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) 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 volatile-qualified) 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 cv-qualified 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);

The behavior of every builtin compound-assignment expression E1 op= E2 is exactly the same as the behavior of the expression E1 = E1 op E2, except that the expression E1 is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. in f(a += b, g()), the += is either not started at all or is completed as seen from inside g()).

[edit] Example

[edit] See also

Operator precedence

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)