1. 程式人生 > >C++對and、or等運算子的支援

C++對and、or等運算子的支援

// test.c
// gcc test.c  --- compile error
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

上面這段c程式碼能編譯通過嗎?答案是不能。gcc test.c

增加一個頭檔案,換成這樣呢:

// test.c
// gcc test.c  --- compile success
#include <stdio.h>
#include <iso646.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}
答案是能!

對於C++,不增加標頭檔案,依然能編譯過下面的test.cpp檔案。

// test.cpp
// g++ test.cpp     ---- compile success<pre name="code" class="cpp">#include <stdio.h>
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

Oh shit, g++什麼時候自動支援這麼”牛逼“的運算子了,這不是php和python的專利嗎?不光是and or,ISO646標準一口氣支援了一堆運算子:

巨集 定義為
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

為什麼這麼做:http://zh.wikipedia.org/wiki/Iso646.h

做到了什麼程度:http://zh.wikipedia.org/wiki/ISO/IEC_646

之所以發本文,是發現OceanBase程式碼中我寫了一個or,被千拂同學review的時候發現。不過,居然沒有被編譯器發現。最近php寫多了是麼?