0

Why can I write:

bool a = sizeof(unsigned int) == sizeof(int);
cout << "(taille unsigned integer = integer) ? " << a;

But this:

cout << "(taille unsigned integer = integer) ? " << sizeof(unsigned int) == sizeof(int);

produces a compilation error?

Invalid operands to binary expression ('std::basic_ostream<char>::__ostream_type' (aka 'basic_ostream<char, std::char_traits<char>>') and 'unsigned long')

Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41

2 Answers2

4

This is an issue of operator precedence. The << operator has higher prcedence than ==, so your expression is parsed as

(cout << "(taille unsigned integer = integer) ? " << sizeof(unsigned int)) == (sizeof(int))

Since the ostream << operator overloads return the ostream they're called on, you're trying to compare a std::ostream to an int, and there is no such comparison.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
3

Due to operator precedence, it is interpreted as:

(cout << "? " << sizeof(unsigned int) ) == sizeof(int);

To solve that, add parentheses around ==:

cout << "? " << (sizeof(unsigned int) == sizeof(int));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Quimby
  • 17,735
  • 4
  • 35
  • 55