Other operators

From cppreference.com
Jump to: navigation, search
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

Contents

[edit] Explanation

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.

[edit] Built-in function call operator

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

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.

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, ...).

[edit] Built-in comma operator

[edit] Built-in conversion operator

The built-in conversion operator performs cpp/language/explicit_cast.

[edit] Conditional operator


[edit] Standard library

[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)

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox