ES.102: Use signed types for arithmetic
ES.102:使用有符号数进行数学运算
Reason(原因)
Because most arithmetic is assumed to be signed; x - y yields a negative number when y > x except in the rare cases where you really want modulo arithmetic.
因为大部分数学运算都是有符号的。当x>y时,x-y会返回一个负数,极少情况实际需要的是按模运算。
Example(示例)
Unsigned arithmetic can yield surprising results if you are not expecting it. This is even more true for mixed signed and unsigned arithmetic.
如果不是你有意为之,无符号运算可能产生意外的结果。如果混用有符号数和无符号数,问题会更明显。
代码语言:javascript复制template<typename T, typename T2>
T subtract(T x, T2 y)
{
return x - y;
}
void test()
{
int s = 5;
unsigned int us = 5;
cout << subtract(s, 7) << 'n'; // -2
cout << subtract(us, 7u) << 'n'; // 4294967294
cout << subtract(s, 7u) << 'n'; // -2
cout << subtract(us, 7) << 'n'; // 4294967294
cout << subtract(s, us 2) << 'n'; // -2
cout << subtract(us, s 2) << 'n'; // 4294967294
}
Here we have been very explicit about what's happening, but if you had seen us - (s 2) or s = 2; ...; us - s, would you reliably have suspected that the result would print as 4294967294?
代码中我们已经很明确地知道发生了什么。但是如果你看到us - (s 2) or s = 2; ...; us - s,你真的可以想象结果是4294967294么?
Exception(例外)
Use unsigned types if you really want modulo arithmetic - add comments as necessary noting the reliance on overflow behavior, as such code is going to be surprising for many programmers.
如果你真的需要按模运算-增加必要的注释提示对溢出行为的依赖,这样的代码会令很多程序员疑惑。
Example(示例)
The standard library uses unsigned types for subscripts. The built-in array uses signed types for subscripts. This makes surprises (and bugs) inevitable.
标准库使用无符号类型作为下标。内置数组使用有符号数作为下标。这会导致代码难于理解并不可避免地带来错误。
代码语言:javascript复制int a[10];
for (int i = 0; i < 10; i) a[i] = i;
vector<int> v(10);
// compares signed to unsigned; some compilers warn, but we should not
for (gsl::index i = 0; i < v.size(); i) v[i] = i;
int a2[-2]; // error: negative size
// OK, but the number of ints (4294967294) is so large that we should get an exception
vector<int> v2(-2);
Use gsl::index for subscripts; see ES.107.
使用ES.107中介绍的gsl::index作为下标。
Enforcement(实施建议)
- Flag mixed signed and unsigned arithmetic
- 标记有符号数和无符号数混用的数学运算。
- Flag results of unsigned arithmetic assigned to or printed as signed.
- 标记将无符号数学运算的结果赋值给有符号数或者作为有符号数print输出的情况。
- Flag negative literals (e.g. -2) used as container subscripts.
- 标记使用负值作为容器下标的情况。
- (To avoid noise) Do not flag on a mixed signed/unsigned comparison where one of the arguments is sizeof or a call to container .size() and the other is ptrdiff_t.
- (为了避免误判)当一个参数是sizeof或者container.size()的返回值,而另一个参数是ptrdiff_t的时候,不要标记有符号数/无符号数混合的比较操作。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es102-use-signed-types-for-arithmetic