未验证 提交 840fa592 编写于 作者: S Sergey Zubkov 提交者: GitHub

merge C.150 in R.23, leaving a redirect behind (#1676)

上级 dd98129f
......@@ -8009,34 +8009,7 @@ Avoid resource leaks.
### <a name="Rh-make_unique"></a>C.150: Use `make_unique()` to construct objects owned by `unique_ptr`s
##### Reason
`make_unique` gives a more concise statement of the construction.
It also ensures exception safety in complex expressions.
##### Example
unique_ptr<Foo> p {new Foo{7}}; // OK: but repetitive
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
// Not exception-safe: the compiler might interleave the computations of arguments as follows:
//
// 1. allocate memory for Foo,
// 2. construct Foo,
// 3. call bar,
// 4. construct unique_ptr<Foo>.
//
// If bar throws, Foo will not be destroyed, and the memory-allocated for it will leak.
f(unique_ptr<Foo>(new Foo()), bar());
// Exception-safe: calls to functions are never interleaved.
f(make_unique<Foo>(), bar());
##### Enforcement
* Flag the repetitive usage of template specialization list `<Foo>`
* Flag variables declared to be `unique_ptr<Foo>`
See [R.23](#Rr-make_unique)
### <a name="Rh-make_shared"></a>C.151: Use `make_shared()` to construct objects owned by `shared_ptr`s
......@@ -9575,11 +9548,14 @@ The `make_shared()` version mentions `X` only once, so it is usually shorter (as
##### Reason
For convenience and consistency with `shared_ptr`.
`make_unique` gives a more concise statement of the construction.
It also ensures exception safety in complex expressions.
##### Note
##### Example
`make_unique()` is C++14, but widely available (as well as simple to write).
unique_ptr<Foo> p {new Foo{7}}; // OK: but repetitive
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
##### Enforcement
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册