C++核心準則R.14: 避免使用[]參數,應該使用span

C++核心準則R.14: 避免使用[]參數,應該使用span

R.14: Avoid [] parameters, prefer span

R.14: 避免使用[]參數,應該使用span

Reason(原因)

An array decays to a pointer, thereby losing its size, opening the opportunity for range errors. Use span to preserve size information.

數組退化成指針,從而失去大小信息,​打開了範圍錯誤的可能性。使用span提供​大小信息。

Example(示例)

<code>void f(int[]);          // not recommended

void f(int*); // not recommended for multiple objects
// (a pointer should point to a single object, do not subscript)

void f(gsl::span); // good, recommended/<code>

Enforcement(實施建議)

Flag [] parameters. Use span instead.

標記[]參數。使用​span參數。

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r14-avoid--parameters-prefer-span​


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

關注【面向對象思考】輕鬆學習每一天!

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


分享到:


相關文章: