日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

發布時間:2025/3/19 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ES.107: Don't use?unsigned?for subscripts, prefer?gsl::index

ES.107:不要使用無符號數下標,使用gsl::index更好

Reason(原因)

To avoid signed/unsigned confusion. To enable better optimization. To enable better error detection. To avoid the pitfalls with?auto?and?int.

為了避免有符號數/無符號數混用帶來的問題。有利實現更好的優化和錯誤檢查。避免auto和int類型帶來的陷阱。

Example, bad(反面示例)

vector vec = /*...*/;
for (int i = 0; i < vec.size(); i += 2) // may not be big enough
cout << vec[i] << '\n';
for (unsigned i = 0; i < vec.size(); i += 2) // risk wraparound
cout << vec[i] << '\n';
for (auto i = 0; i < vec.size(); i += 2) // may not be big enough
cout << vec[i] << '\n';
for (vector::size_type i = 0; i < vec.size(); i += 2) // verbose
cout << vec[i] << '\n';
for (auto i = vec.size()-1; i >= 0; i -= 2) // bug
cout << vec[i] << '\n';
for (int i = vec.size()-1; i >= 0; i -= 2) // may not be big enough
cout << vec[i] << '\n';
Example, good(范例)
vector vec = /*...*/;
for (gsl::index i = 0; i < vec.size(); i += 2) // ok
cout << vec[i] << '\n';
for (gsl::index i = vec.size()-1; i >= 0; i -= 2) // ok
cout << vec[i] << '\n';
Note(注意)

The built-in array uses signed subscripts. The standard-library containers use unsigned subscripts. Thus, no perfect and fully compatible solution is possible (unless and until the standard-library containers change to use signed subscripts someday in the future). Given the known problems with unsigned and signed/unsigned mixtures, better stick to (signed) integers of a sufficient size, which is guaranteed by?gsl::index.

內置數組使用有符號數下標。標準庫容器使用無符號數下標。因此不存在完美、完全兼容的解決方案(除非將來某一天標準庫容器轉而使用有符號數下標)。考慮到使用無符號數或者有符號數/無符號數混合可能帶來的問題,較好的選擇是賦予(有符號)整數足夠大的空間,這一點可以通過使用gsl::index保證。

Example(示例)

template
struct My_container {
public:
// ...
T& operator[](gsl::index i); // not unsigned
// ...
};
Example(示例)
??? demonstrate improved code generation and potential for error detection ???
Alternatives(其他選項)

Alternatives for users

利用者角度的其他選項

  • use algorithms

  • 使用算法

  • use range-for

  • 使用范圍for

  • use iterators/pointers

  • 使用指針和迭代器

Enforcement(實施建議)

  • Very tricky as long as the standard-library containers get it wrong.

  • 如果標準庫容器出問題了,很難檢出。

  • (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#es107-dont-use-unsigned-for-subscripts-prefer-gslindex


覺得本文有幫助?請分享給更多人。

關注微信公眾號【面向對象思考】輕松學習每一天!

面向對象開發,面向對象思考!

總結

以上是生活随笔為你收集整理的无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。