The compiler might or might not warn you if you demote or narrow variables.
Try this:
#include <iostream> int main() { unsigned int i = -5; short x = (short)0x12345678L; std::cout << i << std::endl; std::cout << x << std::endl; }
There is no compiler warning at all.
On the other hand, if you use a {}-initialization, you'll receive a compiler warning about a narrowing conversion.
{}
#include <iostream> int main() { unsigned int i {-5}; std::cout << i << std::endl; }
Yes 🤩, the first method didn't give any warning. The second one gave a compiler error on Xcode.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The compiler might or might not warn you if you demote or narrow variables.
Try this:
There is no compiler warning at all.
On the other hand, if you use a
{}
-initialization, you'll receive a compiler warning about a narrowing conversion.Yes 🤩, the first method didn't give any warning. The second one gave a compiler error on Xcode.