ES.100: Don't mix signed and unsigned arithmetic
ES.100:不要混用有符号数和无符号数
Avoid wrong results.
避免错误的结果。
Example(示例)
int x = -3;
unsigned int y = 7;
cout << x - y << '\n'; // unsigned result, possibly 4294967286
cout << x + y << '\n'; // unsigned result: 4
cout << x * y << '\n'; // unsigned result, possibly 4294967275
It is harder to spot the problem in more realistic examples.
在更加贴近实际开发的代码中发现问题会更加困难。
Note(注意)
Unfortunately, C++ uses signed integers for array subscripts and the standard library uses unsigned integers for container subscripts. This precludes consistency. Use gsl::index for subscripts; see ES.107.
不幸的是,C++使用有符号整数作为数组的下标,而标注库使用无符号整数作为数组的小标,这破坏了一致性原则。使用gsl::index作为下标。参见ES.107。
Enforcement(实施建议)
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es87-dont-add-redundant--or--to-conditions