ES.60:避免在资源管理函数之外使用new和delete
Direct resource management in application code is error-prone and tedious.
在应用代码中直接进行资源管理麻烦且易错。
Note(注意)
This is also known as the rule of "No naked new!"
本准则也被称为“禁止暴露的new操作”
Example, bad(反面示例)
void f(int n)
{
auto p = new X[n]; // n default constructed Xs
// ...
delete[] p;
}
There can be code in the ... part that causes the delete never to happen.
在...省略的位置的代码可以让delete操作永远不发生。
See also: R: Resource management
参见:R:资源管理规则群组
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management
Enforcement(实施建议)
Flag naked news and naked deletes.
标记暴露的new和delete操作。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es60-avoid-new-and-delete-outside-resource-management-functions