Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typo with "cout < myint". Why does it work?

Tags:

c++

iostream

I have this code and I searched for hours why it fails to print my income

int const income = 0;
std::cout << "I'm sorry, your income is: " < income;

Until I found I missed to write << but wrote <. Why doesn't the compiler detect this and error out? I'm not sure why comparing cout makes sense?

like image 333
Johannes Schaub - litb Avatar asked Apr 14 '11 15:04

Johannes Schaub - litb


1 Answers

integral constant 0 is also a null pointer constant - it can be compared to the result of ostream's operator void *. Note that it'll fail if the constant has any value but 0.

like image 69
Erik Avatar answered Oct 15 '22 18:10

Erik