Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of causing a compile error if trying to assign a negative number to an unsigned container?

Is there a way of guarding against the resulting binary from the code in this question? Ideally by way of an error at compile time. Example code from the question:

unsigned int nVal = 0;
nVal = -5;  // no error!
like image 218
Samuel Harmer Avatar asked Sep 20 '11 11:09

Samuel Harmer


1 Answers

If you are using g++, the switch -Wsign-conversion will warn about the conversion, and -Werror will make that warning an error.

like image 159
thiton Avatar answered Sep 30 '22 14:09

thiton