如何让运算优先级浪费你的时间

这个世界本没有优先级,运算符多了,也便有了优先级。

——我也不知道是谁说的

我已经浪费了不知多少时间和奇怪的运算符优先级作斗争。

然而我总是败在它们的脚下。

将我光荣的败绩记录于此,来让我和大家一次又一次地重蹈覆辙。


可爱的位运算们

——你看左移右移运算符那么大肯定优先级贼高嘛!
——我也觉得呢!

——都是位运算,优先级肯定相同嘛!
——当然啦!


更可爱的逻辑运算符们

——编译器那么聪明肯定知道我想要什么嘛!
——就是就是,逻辑那么清晰的!

——你骗我,你上篇文章说的半加器根本没办法工作!

——明明可爱的感叹号打在哪里都无所谓嘛!
——对对对,感叹号和取反没什么区别嘛!


史诗级可爱的大爷们

以下都是各种能让你毁掉一整天的错误。(不要为什么我跪着说出这句话)


暂时就这些了,想到了再写。

总结,如果要想在这些上面浪费时间的话一定要:

  1. 拒绝打括号,尽可能地减少代码长度;
  2. 相信编译器,相信它理解代码的方式一定和你完全一样;
  3. 从不查资料,包括下面这张运算符优先级表。

C++ Operator Precedence

From cppreference

Prec Operator Description Associativity
1 :: Scope resolution Left-to-right
2 a++   a-- Suffix/postfix increment and decrement
type()   type{} Functional cast
a() Function call
a[] Subscript
.   -> Member access
3 ++a   --a Prefix increment and decrement Right-to-left
+a   -a Unary plus and minus
!   ~ Logical NOT and bitwise NOT
(type) C-style cast
*a Indirection (dereference)
&a Address-of
sizeof Size-of[note 1]
new   new[] Dynamic memory allocation
delete   delete[] Dynamic memory deallocation
4 .*   ->* Pointer-to-member Left-to-right
5 a*b   a/b   a%b Multiplication, division, and remainder
6 a+b   a-b Addition and subtraction
7 <<   >> Bitwise left shift and right shift
8 <   <= For relational operators < and ≤ respectively
>   >= For relational operators > and ≥ respectively
9 ==   != For relational operators = and ≠ respectively
10 a&b Bitwise AND
11 ^ Bitwise XOR (exclusive or)
12 | Bitwise OR (inclusive or)
13 && Logical AND
14 || Logical OR
15 a?b:c Ternary conditional[note 2] Right-to-left
throw throw operator
= Direct assignment (provided by default for C++ classes)
+=   -= Compound assignment by sum and difference
*=   /=   %= Compound assignment by product, quotient, and remainder
<<=   >>= Compound assignment by bitwise left shift and right shift
&=   ^=   |= Compound assignment by bitwise AND, XOR, and OR
16 , Comma Left-to-right
  1. The operand of sizeof can’t be a C-style type cast: the expression sizeof (int) * p is unambiguously interpreted as (sizeof(int)) * p, but not sizeof((int)*p).
  2. The expression in the middle of the conditional operator (between ? and <b>:</b>) is parsed as if parenthesized: its precedence relative to ?: is ignored.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

ˆ Back To Top