[英]Why is "sizeof(unsigned int) == sizeof(int)" refused in a cout?
為什么我可以寫:
bool a = sizeof(unsigned int) == sizeof(int);
cout << "(taille unsigned integer = integer) ? " << a;
但是這個:
cout << "(taille unsigned integer = integer) ? " << sizeof(unsigned int) == sizeof(int);
產生編譯錯誤?
Invalid operands to binary expression ('std::basic_ostream<char>::__ostream_type' (aka 'basic_ostream<char, std::char_traits<char>>') and 'unsigned long')
這是運算符優先級的問題。 <<
運算符的優先級高於==
,因此您的表達式被解析為
(cout << "(taille unsigned integer = integer) ? " << sizeof(unsigned int)) == (sizeof(int))
由於ostream
<<
運算符重載返回它們被調用的ostream
,因此您試圖將std::ostream
與int
進行比較,並且沒有這樣的比較。
由於運算符優先級,請在==
周圍添加括號。
沒有它們,它被解釋為:
(cout << "? " << sizeof(unsigned int) ) == sizeof(int);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.