According to cppreference.com, the C++ static_cast
operator's level of precedence is 2.
Why are those levels even defined? I can't think of any reason. Can anyone provide an example?
The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3 , the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary.
The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.
Operator precedence determines which operation is performed first in an expression with more than one operators with different precedence. Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.
Precedence is the priority for grouping different types of operators with their operands. Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.
The standard doesn't define precedence levels; these can be derived from the grammar.
Like any other syntactical feature, static_cast
has a place in this grammar. Because its use requires parentheses its operand expression can never be ambiguous, but that only means that it makes no sense to bother deriving a precedence level for it from the grammar, not that its place in the grammar itself is meaningless. Thus the standard is doing nothing crazy here.
What's pointless is that whatever source you cited listed a precedence level for static_cast
. It's not wrong, it's just pointless.
The C++ cast operator’s level of precedence is 2
Who said this? The standard doesn't define operator precedence. It defines the grammar in a BNF-like notation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With