C++核心準則ES.102:使用有符號數(shù)進行數(shù)學運算

ES.102: Use signed types for arithmetic
ES.102:使用有符號數(shù)進行數(shù)學運算
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.
因為大部分數(shù)學運算都是有符號的。當x>y時,x-y會返回一個負數(shù),極少情況實際需要的是按模運算。
Example(示例)
Unsigned arithmetic can yield surprising results if you are not expecting it. This is even more true for mixed signed and unsigned arithmetic.
如果不是你有意為之,無符號運算可能產(chǎn)生意外的結(jié)果。如果混用有符號數(shù)和無符號數(shù),問題會更明顯。
template
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?
代碼中我們已經(jīng)很明確地知道發(fā)生了什么。但是如果你看到us - (s + 2) or s += 2; ...; us - s,你真的可以想象結(jié)果是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.
標準庫使用無符號類型作為下標。內(nèi)置數(shù)組使用有符號數(shù)作為下標。這會導致代碼難于理解并不可避免地帶來錯誤。
int a[10];
for (int i = 0; i < 10; ++i) a[i] = i;
vector 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 v2(-2);
Use?gsl::index?for subscripts;?see ES.107.
使用ES.107中介紹的gsl::index作為下標。
Enforcement(實施建議)
Flag mixed signed and unsigned arithmetic
標記有符號數(shù)和無符號數(shù)混用的數(shù)學運算。
Flag results of unsigned arithmetic assigned to or printed as signed.
標記將無符號數(shù)學運算的結(jié)果賦值給有符號數(shù)或者作為有符號數(shù)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.
(為了避免誤判)當一個參數(shù)是sizeof或者container.size()的返回值,而另一個參數(shù)是ptrdiff_t的時候,不要標記有符號數(shù)/無符號數(shù)混合的比較操作。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es102-use-signed-types-for-arithmetic
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學習每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
