The importance of declaring a variable as unsigned

前端 未结 14 3262
臣服心动
臣服心动 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:18

    It doesn't prevent people misusing your interface, but at least they should get a warning unless they add a C-style cast or static_cast to make it go away (in which case you cannot help them further).

    Yes, there is value in this as it properly expresses the semantics you wish.

提交回复
热议问题