Operators in C and C++: Difference between revisions

Content deleted Content added
Info that was illogically/incorrectly under evaluation order is moved to intro as it's pretty high-level info
m Relational: Fixed table spanning incorrectly.
 
(3 intermediate revisions by 3 users not shown)
Line 69:
| {{rh}} colspan="2" | Postfix increment
| align="center" | <code>a'''++'''</code>
| {{cpp|1=R K::operator ++(int);}} {{efn|name=dummy-int|The {{cpp|int}} is a dummy parameter to differentiate between prefix and postfix.}}
| {{cpp|1=R operator ++(K& a, int);}} {{efn|name=dummy-int}}
|-
| {{rh}} colspan="2" | Prefix [[Increment and decrement operators|decrement]]
Line 79:
| {{rh}} colspan="2" | Postfix decrement
| align="center" | <code>a'''--'''</code>
| {{cpp|1=R K::operator --(int);}} {{efn|name=dummy-int}}
| {{cpp|1=R operator --(K& a, int);}} {{efn|name=dummy-int}}
|}
 
Line 128:
|-
| {{rh}} colspan="2" | [[Three-way comparison]]<ref name="threewaycomparison" group="lower-alpha"/>{{efn|Possible return types: <code>std::weak_ordering</code>, <code>std::strong_ordering</code> and <code>std::partial_ordering</code> to which they all are convertible to.}}
| rowspan="2" style="text-align:center;" | <code>a '''&lt;=&gt;''' b</code> || rowspan="2"{{no}}
| {{cpp|1=auto K::operator <=>(const S &b);}}
| {{cpp|1=auto operator <=>(const K &a, const S &b);}}
Line 175:
! outside class
|-
| {{rh}} colspan="2" | [[Bitwise operation#NOT|Bitwise NOT]]
| align="center" | <code>'''~'''a</code><br/>
| {{cpp|1=R K::operator ~();}}
| {{cpp|1=R operator ~(K a);}}
|-
| {{rh}} colspan="2" | [[Bitwise operation#AND|Bitwise AND]]
| style="text-align:center;" | <code>a '''&''' b</code>
| {{cpp|1=R K::operator &(S b);}}
| {{cpp|1=R operator &(K a, S b);}}
|-
| {{rh}} colspan="2" | [[Bitwise operation#OR|Bitwise OR]]
| style="text-align:center;" | <code>a '''<nowiki>|</nowiki>''' b</code>
| {{cpp|1=R K::operator {{!}}(S b);|lang=cpp}}
| {{cpp|1=R operator {{!}}(K a, S b);|lang=cpp}}
|-
| {{rh}} colspan="2" | [[Bitwise operation#XOR|Bitwise XOR]]
| style="text-align:center;" | <code>a '''^''' b</code>
| {{cpp|1=R K::operator ^(S b);}}
| {{cpp|1=R operator ^(K a, S b);}}
|-
| {{rh}} colspan="2" | [[Bitwise shift|BitwiseShift]] left shift]]<ref name="bitshift" group="lower-alpha" />
| style="text-align:center;" | <code>a '''<<''' b</code>
| {{cpp|1=R K::operator <<(S b);}}
| {{cpp|1=R operator <<(K a, S b);}}
|-
| {{rh}} colspan="2" | [[Bitwise shift|BitwiseShift right shift]]<ref name="bitshift" group="lower-alpha" />{{Refn | Operation="rightbitshift" | group="lower-alpha" | According to the C99 standard, the right shift of a negative number is implementation defined. Most implementations, e.g., the GCC,<ref name="Integers">{{Citation | contribution = Integers implementation | url = //gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Integers-implementation.html#Integers-implementation | title = GCC 4.3.3 | publisher = GNU}}.</ref> use an [[arithmetic shift]] (i.e., sign extension), but a [[logical shift]] is possible.}}
| style="text-align:center;" | <code>a '''>>''' b</code>
| {{cpp|1=R K::operator >>(S b);}}
Line 210:
C and C++ have the same assignment operators and all can be overloaded in C++.
 
For the combination operators, <code>a ⊚= b</code> (where <code>⊚</code> represnetsrepresents an operation) is equivalent to <code>a = a ⊚ b</code>, except that <code>a</code> is evaluated only once.
 
{| class="wikitable" style="width:100%"
Line 384:
| {{n/a}}
|-
| {{rh}} colspan="2" | [[type conversion|Conversion]] {{efn|Behaves like const_cast/static_cast/reinterpret_cast. In the last two cases, the <code>auto</code> specifier is replaced with the type of the invented variable x declared with <code>auto x(a);</code> (which is never interpreted as a function declaration) or <code>auto x{a};</code>, respectively.}}<ref>[https://en.cppreference.com/w/cpp/language/explicit_cast Explicit type conversion] in C++</ref>
| style="text-align:center;" | <code>R(a)</code><br><code>R{a}</code><ref name="sinceCXX11" group="lower-alpha" /><br><code>auto(a)</code><ref name="sinceCXX23" group="lower-alpha" /><br><code>auto{a}</code><ref name="sinceCXX23" group="lower-alpha" /> || {{no}} || {{no}}
| {{rh}} colspan="2" {{n/a}}
|-
| {{rh}} colspan="2" | [[static_cast]] conversion {{efn|For user-defined conversions, the return type implicitly and necessarily matches the operator name unless the type is inferred (e.g. {{cpp|1=operator auto()}}, {{cpp|1=operator decltype(auto)()}} etc.).}}
| style="text-align:center;" | <code>'''static_cast'''<R>(a)</code> || {{yes}} || {{no}}
| {{cpp|1=K::operator R();}}<br>{{cpp|1=explicit K::operator R();}}<ref name="sinceCXX11" group="lower-alpha" />