下面的代码:可以在C++流中使用三元运算符吗?
#include <iostream>
using namespace std;
struct A{int number=10;};
int main()
{
A* a = new A();
cout<<"A val: "<< a==nullptr?"":a->number<<endl;
}
编译海合会4.7和C++ 11给出:
error: invalid operands of types 'int' and '' to binary 'operator <<'
我不明白为什么,会是正确的方式做到这一点呢?我希望尽可能缩短空值检查,因为我期望它们非常普遍。
对于大多数人来说,三元运算符具有[令人惊讶的低优先级](http://en.cppreference.com/w/cpp/language/operator_precedence)。我的建议:避免它,只要你不确定它的作用:你的同事会感谢你。 – Wolf