Operatori in C e C++: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica
Riga 45:
 
==== Tabella delle precedenze ====
Nella tabella seguente sono elencati tutti gli operatori in base alla loro precedenza. Essi sono sempre implementabili su C++, mentre alcuni non lo sono in C.
{| class="wikitable"
|-
! style="text-align: left" | Precedence
! style="text-align: left" | Operator
! style="text-align: left" | Description
! style="text-align: left" | Associativity
|-
! 1
<small>highest</small>
| <code>::</code>
| [[Scope resolution operator#C.2B.2B|Scope resolution]] (C++ only)
| None
|-
! rowspan=11| 2
| style="border-bottom-style: none" | <code>++</code>
| style="border-bottom-style: none" | Postfix increment
| style="vertical-align: top" rowspan="11" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>--</code>
| style="border-bottom-style: none; border-top-style: none" | Postfix decrement
|-
| style="border-bottom-style: none; border-top-style: none" | <code>()</code>
| style="border-bottom-style: none; border-top-style: none" | Function call
|-
| style="border-bottom-style: none; border-top-style: none" | <code>[]</code>
| style="border-bottom-style: none; border-top-style: none" | Array subscripting
|-
| style="border-bottom-style: none; border-top-style: none" | <code>.</code>
| style="border-bottom-style: none; border-top-style: none" | Element selection by reference
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-&gt;</code>
| style="border-bottom-style: none; border-top-style: none" | Element selection through pointer
|-
| style="border-bottom-style: none; border-top-style: none" | <code>typeid()</code>
| style="border-bottom-style: none; border-top-style: none" | [[Run-time type information]] (C++ only) (see [[typeid]])
|-
| style="border-bottom-style: none; border-top-style: none" | <code>const_cast</code>
| style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[const_cast]])
|-
| style="border-bottom-style: none; border-top-style: none" | <code>dynamic_cast</code>
| style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[dynamic cast]])
|-
| style="border-bottom-style: none; border-top-style: none" | <code>reinterpret_cast</code>
| style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[reinterpret_cast]])
|-
| style="border-top-style: none" | <code>static_cast</code>
| style="border-top-style: none" | Type cast (C++ only) (see [[static_cast]])
|-
! rowspan="13" | 3
| style="border-bottom-style: none" | <code>++</code>
| style="border-bottom-style: none" | Prefix increment
| rowspan="13" style="vertical-align: top" | Right-to-left
|-
| style="border-bottom-style: none; border-top-style: none" | <code>--</code>
| style="border-bottom-style: none; border-top-style: none" | Prefix decrement
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+</code>
| style="border-bottom-style: none; border-top-style: none" | Unary plus
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-</code>
| style="border-bottom-style: none; border-top-style: none" | Unary minus
|-
| style="border-bottom-style: none; border-top-style: none" | <code>!</code>
| style="border-bottom-style: none; border-top-style: none" | Logical NOT
|-
| style="border-bottom-style: none; border-top-style: none" | <code>~</code>
| style="border-bottom-style: none; border-top-style: none" | Bitwise NOT ([[ones' complement]])
|-
| style="border-bottom-style: none; border-top-style: none" | <code>(''type'')</code>
| style="border-bottom-style: none; border-top-style: none" | Type cast
|-
| style="border-bottom-style: none; border-top-style: none" | <code>*</code>
| style="border-bottom-style: none; border-top-style: none" | Indirection (dereference)
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&</code>
| style="border-bottom-style: none; border-top-style: none" | Address-of
|-
| style="border-bottom-style: none; border-top-style: none" | <code>sizeof</code>
| style="border-bottom-style: none; border-top-style: none" | [[Sizeof]]
|-
| style="border-bottom-style: none; border-top-style: none" | <code>_Alignof</code>
| style="border-bottom-style: none; border-top-style: none" | Alignment requirement (since C11)
|-
| style="border-bottom-style: none; border-top-style: none" | <code>new</code>, <code>new[]</code>
| style="border-bottom-style: none; border-top-style: none" | Dynamic memory allocation (C++ only)
|-
| style="border-top-style: none" | <code>delete</code>, <code>delete[]</code>
| style="border-top-style: none" | Dynamic memory deallocation (C++ only)
|-
! rowspan=2| 4
| style="border-bottom-style: none" | <code>.*</code>
| style="border-bottom-style: none" | Pointer to member (C++ only)
| style="vertical-align: top" rowspan="2" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>->*</code>
| style="border-bottom-style: none; border-top-style: none" | Pointer to member (C++ only)
|-
! rowspan=3| 5
| style="border-bottom-style: none" | <code>*</code>
| style="border-bottom-style: none" | Multiplication
| style="vertical-align: top" rowspan="3" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>/</code>
| style="border-bottom-style: none; border-top-style: none" | Division
|-
| style="border-bottom-style: none; border-top-style: none" | <code>%</code>
| style="border-bottom-style: none; border-top-style: none" | [[Modulo operation|Modulo]] (remainder)
|-
! rowspan=2| 6
| style="border-bottom-style: none" | <code>+</code>
| style="border-bottom-style: none" | Addition
| style="vertical-align: top" rowspan="2" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-</code>
| style="border-bottom-style: none; border-top-style: none" | Subtraction
|-
! rowspan=2| 7
| style="border-bottom-style: none" | <code>&lt;&lt;</code>
| style="border-bottom-style: none" | [[Bitwise operation|Bitwise]] left shift
| style="vertical-align: top" rowspan="2" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;&gt;</code>
| style="border-bottom-style: none; border-top-style: none" | [[Bitwise operation|Bitwise]] right shift
|-
! rowspan=1| 8
| style="border-bottom-style:none;" | <code>&lt;=&gt;</code>
| style="border-bottom-style:none;" | [[Three-way comparison]] (Introduced in [[C++20]] - C++ only)
| style="vertical-align: top" rowspan="1" | Left-to-right
|-
! rowspan=4| 9
| style="border-bottom-style: none" | <code>&lt;</code>
| style="border-bottom-style: none" | Less than
| style="vertical-align: top" rowspan="4" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&lt;=</code>
| style="border-bottom-style: none; border-top-style: none" | Less than or equal to
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;</code>
| style="border-bottom-style: none; border-top-style: none" | Greater than
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;=</code>
| style="border-bottom-style: none; border-top-style: none" | Greater than or equal to
|-
! rowspan=2| 10
| style="border-bottom-style: none" | <code>==</code>
| style="border-bottom-style: none" | Equal to
| style="vertical-align: top" rowspan="2" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>!=</code>
| style="border-bottom-style: none; border-top-style: none" | Not equal to
|-
! 11
| <code>&</code>
| Bitwise AND
| Left-to-right
|-
! 12
| <code>^</code>
| Bitwise XOR (exclusive or)
| Left-to-right
|-
! 13
| <code><nowiki>|</nowiki></code>
| Bitwise OR (inclusive or)
| Left-to-right
|-
! 14
| <code>&&</code>
| Logical AND
| Left-to-right
|-
! 15
| <code><nowiki>||</nowiki></code>
| Logical OR
| Left-to-right
|-
! rowspan="2" | 16
| style="border-bottom-style: none" | <code>co_await</code>
| rowspan="2" | Coroutine processing (C++ only)
| rowspan="2" | Right-to-left
|-
| style="border-top-style: none" | <code>co_yield</code>
|-
! rowspan="13" | 17
| style="border-bottom-style: none" | <code>?:</code>
| style="border-bottom-style: none" | [[Ternary conditional operator]]
| rowspan="13" | Right-to-left
|-
| style="border-bottom-style: none; border-top-style: none" | <code>=</code>
| style="border-bottom-style: none; border-top-style: none" | Direct assignment
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by sum
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by difference
|-
| style="border-bottom-style: none; border-top-style: none" | <code>*=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by product
|-
| style="border-bottom-style: none; border-top-style: none" | <code>/=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by quotient
|-
| style="border-bottom-style: none; border-top-style: none" | <code>%=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by remainder
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&lt;&lt;=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise left shift
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;&gt;=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise right shift
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise AND
|-
| style="border-bottom-style: none; border-top-style: none" | <code>^=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise XOR
|-
| style="border-bottom-style: none; border-top-style: none" | <code><nowiki>|</nowiki>=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise OR
|-
| style="border-top-style: none" | <code>throw</code>
| style="border-top-style: none" | Throw operator (exceptions throwing, C++ only)
|-
! 18
<small>lowest</small>
| <code>,</code>
| [[Comma operator|Comma]]
| Left-to-right
|}
<ref>{{Cite web|url=https://en.cppreference.com/w/c/language/operator_precedence|title=C Operator Precedence - cppreference.com|website=en.cppreference.com|access-date=2019-07-16}}</ref><ref>{{cite web |title=C++ Built-in Operators, Precedence and Associativity |url=https://docs.microsoft.com/en-US/cpp/cpp/cpp-built-in-operators-precedence-and-associativity |website=docs.microsoft.com |access-date=11 May 2020 |language=en-us}}</ref><ref>{{cite web|url=https://en.cppreference.com/w/cpp/language/operator_precedence|title=C++ Operator Precedence - cppreference.com|website=en.cppreference.com|access-date=2019-07-16}}</ref>
 
== Tabelle degli operatori ==