为什么下面的代码输出'0' ?
#include <bits/stdc++.h>
int main() {
int max = 5;
std::cout << (false) ? "impossible" : std::to_string(max);
}
语句
std::cout << false ? "impossible" : std::to_string(max);
等价于
(std::cout << false) ? "impossible" : std::to_string(max);
因为<<
的优先级高于?:
,false
被打印为0
。
你可能预料到这个
std::cout << (false ? "impossible" : std::to_string(max));
您应该阅读操作符优先级以避免此类意外。