Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
std::cout << (-1 < 0) << ' ' << (-1 < 0u) << '\n';
}1 0
- Expecting
1 1: in-1 < 0utheintis converted tounsignedfirst, and-1becomes the largestunsignedvalue, which is not less than0. - Expecting undefined behavior: converting a negative
inttounsignedis well defined, and-1always becomes the largestunsignedvalue.
- Which warning flag makes GCC and Clang report a comparison of an
intwith anunsigned? - How does C++20's
std::cmp_less(-1, 0u)compare the two values, and what does it return?