Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is "sizeof(unsigned int) == sizeof(int)" refused in a cout?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading