使用了非标准扩展_C++核心准则ES.27:使用std::array或stack_array在堆栈上构建数组

25026cfa8b8e99ad1fc5b82cb7e64395.png

ES.27: Use std::array or stack_array for arrays on the stack

ES.27:使用std::array或者stack_array在堆栈上构建数组

Reason(原因)

They are readable and don't implicitly convert to pointers. They are not confused with non-standard extensions of built-in arrays.

它们的可读性好,而且不会隐式转换为指针类型。它们不会和内置数组的非标准扩展相混淆。

Example, bad(反面示例)

const int n = 7;int m = 9;void f(){  int a1[n];  int a2[m];   // error: not ISO C++      // ...}

Note(注意)

The definition of a1 is legal C++ and has always been. There is a lot of such code. It is error-prone, though, especially when the bound is non-local. Also, it is a "popular" source of errors (buffer overflow, pointers from array decay, etc.). The definition of a2 is C but not C++ and is considered a security risk.

a1的定义是一直都是合法的C++语法。存在很多这样的代码。虽然它容易出错误,特别是边界不是局部变量时。同时它也是很多错误的常见原因(缓冲区溢出,退化数组的指针等)。a2是C语法而不是C++语法。在C++中被认为存在安全风险。

Example(示例)

const int n = 7;int m = 9;void f(){    array a1;    stack_array a2(m);    // ...}
Enforcement(实施建议)
  • Flag arrays with non-constant bounds (C-style VLAs)
  • 标记变长数组(C风格不定长数组)
  • Flag arrays with non-local constant bounds
  • 标记非局部常量定义长度的数组。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es27-use-stdarray-or-stack_array-for-arrays-on-the-stack


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

97ba8a41d567c8f7208673f61fc48cae.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值