Other operators

< cpp‎ | language
Revision as of 09:26, 23 April 2013 by P12 (Talk | contribs)

C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Alternative representations
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Attributes (C++11)
typedef declaration
Type alias declaration (C++11)
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Class-specific function properties
Special member functions
Operator name Syntax Over​load​able Prototype examples (for class T)
Inside class definition Outside class definition
function call a(a1, a2) Yes R T::operator()(Arg1 &a1, Arg2 &a2, ... ...); N/A
comma a, b Yes T2& T::operator,(T2 &b); T2& operator,(const T &a, T2 &b);
conversion (type) a Yes operator type() N/A
ternary conditional a ? b : c No N/A N/A



function call operator provides function semantics for any object.

conversion operator converts given type to another type. The name of the operator must be the same as the type intended to be returned.

ternary conditional operator checks the boolean value of the first expression and replaces entire operator clause with the second or the third expression depending on the resulting value.

Built-in function call operator

A function call expression, such as E(A1, A2, A3), consists of an expression that names the function, E, followed by a possibly empty list of expressions A1, A2, A3, ..., in parentheses.

The expression that names the function can be

a) lvalue expression that refers to a function
b) pointer to function
c) explicit class member access expression that selects a member function
d) implicit class member access expression, e.g. member function name used within another member function.

The function (or member) name specified by E can be overloaded, overload resolution rules used to decide which overload is to be called.

If E specifies a member function, it may be virtual, in which case the final overrider of that function will be called, using dynamic dispatch at runtime.

To call the function, all expressions A1, A2, A3, etc, provided as arguments are evaluated in arbitrary order, and each function parameter is initialized with its corresponding argument after implicit conversion if neccessary. If the call is made to a member function, then the this pointer to current object is converted as if by explicit cast to the this pointer expected by the function. The initialization and destruction of each parameter occurs in the context of the caller, which means, for example, that if constructor of a parameter throws an exception, the exception handlers defined within the function, even as a function-try block, are not considered. If the function is a variadic function, default argument promotions are applied to all arguments matched by the ellipsis parameter.

The return type of a function call expression is the return type of the chosen function, decided using static binding (ignoring the virtual) keyword), even if the overriding function that's actually called returns a different type. This allows the overriding functions to return pointers or references to classes that are derived from the return type returned by the base function, i.e. C++ supports covariant return types). If E specifies a destructor, the return type is void.

The value category of a function call expression is lvalue if the function returns an lvalue reference or an rvalue reference to function, is an xvalue if the function returns an rvalue reference to object, and is a prvalue otherwise. If the function call expression is a prvalue of object type, it must have complete type except when used as an operand to decltype.

Function call expression is similar in syntax to value initialization T(), to function-style cast expression T(A1), and to direct initialization of a temporary T(A1, A2, A3, ...), where T is the name of a type.

#include <cstdio>
struct S
    int f1(double d) {
        printf("%f \n", d); // variable argument function call
    int f2() {
        f1(7); // member function call, same as this->f1()
               // integer argument converted to double
void f() {
   puts("function called"); // function call
int main()
    f(); // function call
    S s;
    s.f2(); // member function call


function called

Built-in comma operator

In a comma expression E1, E2, the expression E1 is evaluated, its return value is discarded, and its side effects are completed before evaluation of the expression E2 begins (note that this ability is lost with user-defined operator,).

The return type and value category of the comma operator are exactly the return type and the value category of the second operand, E2.

The comma in various comma-separated lists, such as function argument lists (f(a, b, c)), initializer lists int a[] = {1,2,3}, or initialization statements (int i, j;) is not the comma operator. If the comma operator needs to be used in that context, it has to be parenthesized: f(a, (n++, n+b), c)

#include <iostream>
int main()
    int n = 1;
    int m = (++n, std::cout << "n = " << n << '\n', ++n, 2*n);
    std::cout << "m = " << (++m, m) << '\n';


n = 2
m = 7

Built-in conversion operator

The built-in conversion operator (T)expr evaluates the expression expr and performs explicit cast to the type T.

See explicit cast for detailed description.

Conditional operator

For every pair of promoted arithmetic types L and R and for every type P, where P is a pointer, pointer-to-member, or scoped enumeration type, the following function signatures participate in overload resolution: Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
LR operator?:(bool, L, R );

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
T operator?:(bool, T, T );

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end where LR is the result of usual arithmetic conversions performed on L and R. The operator “?:” cannot be overloaded, these function signatures only exist for the purpose of overload resolution.

The first operand of the conditional operator is evaluated and contextually converted to bool. After both the value evaluation and all side effects of the first operand are completed, if the result was true, the second operand is evaluated. If the result was false, the third operand is evaluated.

In the conditional expression E1 ? E2 : E3, the following rules and limitations apply:

1) If either E2 or E3 (or both) is a throw-expression, the result of the conditional operator is the result of the other (not throw) expression, and is a prvalue (after lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion). Such conditional operator is commonly used in constexpr programming.
2) If both E2 or E3 are of type void, the result is a prvalue of type void.
3) E2 and E3 have different class types (or same type with different cv-qualification) and the same value category. In this case, an attempt is made to convert one (and only one) of the operands to the type of the other, as follows:
a) If they are lvalues, an implicit conversion to the lvalue reference type is attempted
b) If they are xvalues, an implicit conversion to the rvalue reference type is attempted
c) If they are rvalues, and have the same base class (or one is a base class of the other), the operand(s) are converted to the base type by copy-initializing a temporary object of the base type.
d) If they are rvalues, and have no common base class, then an implicit conversion is attempted to the type of the other operand.
4) Both E2 or E3 are glvalues of the same type. In this case, the result has the same type and value category.
5) If all cases listed above fail, and either E2 or E3 has class type: overload resolution is attempted to select the best conversion from one type to the other.
6) If all cases listed above fail, and neither E2 nor E3 have class type: first, lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions are applied. Then,
a) if both E2 and E3 now have the same type, the result is a prvalue temporary of that type, copy-initialized from whatever operand was selected after evaluating E1
b) E2 and E3 have arithmetic or enumeration type: usual arithmetic conversions are applied to bring them to common type, that type is the result.
c) E2 and E3 are pointers, or a pointer and a null constant, or a both null pointer constants, one of which is a std::nullptr_t, then pointer conversions and qualification convrsions are applied to bring them to common type, that type is the result.
d) E2 and E3 are pointers to members, or a pointer to member and a null constant: then pointer-to-member conversions and qualification convrsions are applied to bring them to common type, that type is the result.

The return type of a conditional operator is also accessible as the binary type trait std::common_type

#include <string>
#include <stdexcept>
struct Node
    Node* next;
    int data;
    // deep-copying copy constructor
    Node(const Node& other)
      : next( ? new Node(* : NULL)
      , data(
    Node(int d) : next(NULL), data(d) {}
    ~Node() { delete next ; }
int main()
    // simple rvalue example
    int n = 1>2 ? 10 : 11;  // 1>2 is false, so n = 11
    // simple lvalue example
    int m = 10; 
    (n == m ? n : m) = 7; // n == m is false, so m = 7
    // throw example
    std::string str = 2+2==4 ? "ok" : throw std::logic_error("2+2 != 4");

Standard library

Many classes in the standard library override operator() to be used as function objects.

Template:cpp/locale/locale/dcl list operator()Template:cpp/container/value compare/dcl list operator()Template:cpp/container/value compare/dcl list operator()Template:cpp/thread/packaged task/dcl list operator()Template:cpp/numeric/random/engine/dcl list operator()Template:cpp/numeric/random/distribution/dcl list operator()
deletes the object or array
(public member function of std::default_delete)
compares its arguments using owner-based semantics
returns the sum of two arguments
(public member function of std::plus) [edit]
returns the difference between two arguments
(public member function of std::minus) [edit]
returns the product of two arguments
(public member function of std::multiplies) [edit]
returns the result of the division of the first argument by the second argument
(public member function of std::divides) [edit]
returns the remainder from the division of the first argument by the second argument
(public member function of std::modulus) [edit]
returns the negation of the argument
(public member function of std::negate) [edit]
checks if the arguments are equal
(public member function of std::equal_to) [edit]
checks if the arguments are not equal
(public member function of std::not_equal_to) [edit]
checks if the first argument is greater than the second
(public member function of std::greater) [edit]
checks if the first argument is less than the second
(public member function of std::less) [edit]
checks if the first argument is greater than or equal to the second
(public member function of std::greater_equal) [edit]
checks if the first argument is less than or equal to the second
(public member function of std::less_equal) [edit]
returns the logical AND of the two arguments
(public member function of std::logical_and) [edit]
returns the logical OR of the two arguments
(public member function of std::logical_or) [edit]
returns the logical NOT of the argument
(public member function of std::logical_not) [edit]
returns the result of bitwise AND of two arguments
(public member function of std::bit_and) [edit]
returns the result of bitwise OR of two arguments
(public member function of std::bit_or) [edit]
returns the result of bitwise XOR of two arguments
(public member function of std::bit_xor) [edit]
returns the logical complement of the result of a call to the stored predicate
(public member function of std::unary_negate) [edit]
returns the logical complement of the result of a call to the stored predicate
(public member function of std::binary_negate) [edit]
calls the stored function
(public member function of std::reference_wrapper) [edit]
invokes the target
(public member function of std::function) [edit]

Several standard library classes provide user-defined conversion functions

Template:cpp/error/error code/dcl list operator boolTemplate:cpp/error/error condition/dcl list operator boolTemplate:cpp/container/vector bool/reference/dcl list operator boolTemplate:cpp/memory/unique ptr/dcl list operator boolTemplate:cpp/memory/shared ptr/dcl list operator boolTemplate:cpp/io/basic ios/dcl list operator boolTemplate:cpp/regex/sub match/dcl list strTemplate:cpp/atomic/atomic/dcl list operator TTemplate:cpp/memory/auto ptr/dcl list operator auto ptr
returns the referenced bit
(public member function of std::bitset::reference) [edit]
accesses the stored reference
(public member function of std::reference_wrapper) [edit]
checks if a valid target is contained
(public member function of std::function) [edit]
returns the wrapped value
(public member function of std::integral_constant) [edit]
tests whether the lock owns its associated mutex
(public member function of std::unique_lock) [edit]

The comma operator is not overloaded by any class in the standard library. The boost library uses operator, in boost.assign, boost.spirit, and other libraries.

See also

Operator precedence

Common operators
assignment increment
arithmetic logical comparison member

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


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

a && b
a || b

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


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)