mercredi 27 avril 2016

Is it ok to use ternary operator in C++ streams?

The following code:

#include <iostream>
using namespace std;
struct A{int number=10;};

int main()
{
   A* a = new A();
   cout<<"A val: "<< a==nullptr?"":a->number<<endl;
}

compiling on gcc 4.7 with c++11 gives:

error: invalid operands of types 'int' and '' to binary 'operator <<'

I do not understand why, what would be to proper way to do it? I want to keep null checks as short as possible as I expect them to be very common.

Aucun commentaire:

Enregistrer un commentaire