我看到一些代码似乎使用了一个我无法识别的运算符,以两个感叹号的形式出现,如下所示:!!.有人可以告诉我这个运营商的作用吗?
我看到这个的背景是,
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
Run Code Online (Sandbox Code Playgroud) 我一直在阅读Linux内核(特别是2.6.11).我发现了以下定义:
#define unlikely(x) __builtin_expect(!!(x), 0)
Run Code Online (Sandbox Code Playgroud)
(来自linux-2.6.11/include/linux/compiler.h:61 lxr链接)
什么!! 完成?为什么不使用(x)?
也可以看看:
bool在评估if()语句中的条件时,C++的现代版本尝试使用类的运算符.其他铸造操作员,例如int在没有bool操作员时使用.这在下面说明.
#include <iostream>
using namespace std;
class TwoInts {
public:
int a,b;
operator bool() { cout << "TwoInts to bool" << endl; return 0;}
operator int() { cout << "TwoInts to int" << endl; return 0;}
};
class SixInts {
public:
int a[6];
operator int() { cout << "SixInts to int" << endl; return 0;}
};
int main(void) {
TwoInts T;
SixInts S;
if (T) cout << "xxx" << endl;
if (S) cout …Run Code Online (Sandbox Code Playgroud)