The importance of declaring a variable as unsigned

前端 未结 14 3340
臣服心动
臣服心动 2021-02-20 05:55

Is it important to declare a variable as unsigned if you know it should never be negative? Does it help prevent anything other than negative numbers being fed into a function th

14条回答
  •  悲哀的现实
    2021-02-20 06:29

    It also keeps you from having to cast to/from unsigned whatever when interacting with other interfaces. For example:

    for (int i = 0; i < some_vector.size(); ++i)
    

    That will generally annoy the hell out of anyone who needs to compile without warnings.

提交回复
热议问题