File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -154,7 +154,11 @@ shared_ptr<Foo> foo(new Foo());
154
154
auto foo = make_shared<Foo>();
155
155
```
156
156
157
- > 从 C++14 开始,内存安全的现代 C++ 程序中就不会出现任何显式的 new 了,哪怕是抱在 shared_ptr 或 unique_ptr 内的也不行。(除了最上面说的 3 种特殊情况)
157
+ > {{ icon.detail }} 有趣的是,make_shared 在 C++11 就引入了,make_unique 却直到 C++14 才引入。
158
+
159
+ 从 C++14 开始,内存安全的现代 C++ 程序中就不会出现任何显式的 new 了,哪怕是包在 shared_ptr 或 unique_ptr 内的也不行。(除了最上面说的 3 种特殊情况)
160
+
161
+ ### 贴士 2.3
158
162
159
163
如果你需要调用的 C 语言接口还需要原始指针的话,用 ` .get() ` 可以从智能指针中获取原始指针。建议只在和 C 语言打交道时 ` .get() ` ,其余时间一律 shared_ptr 保证安全。
160
164
@@ -328,3 +332,26 @@ void t1() {
328
332
- 你可以两个线程同时读取同一个全局的原始指针变量,同样地,shared_ptr 也可以,有任何区别吗?
329
333
330
334
反正,shared_ptr 内部专门为线程安全做过设计,你不用去操心。
335
+
336
+ ## placement new
337
+
338
+ placement new 和 placement delete 也可以用 std::construct_at 和 std::destroy_at 代替:
339
+
340
+ ``` cpp
341
+ #include < new>
342
+
343
+ struct Foo {
344
+ explicit Foo(int age) { ... }
345
+ Foo(Foo &&) = delete;
346
+ ~ Foo() { ... }
347
+ };
348
+
349
+ void func() {
350
+ alignas(Foo) unsigned char buffer[ sizeof(Foo)] ;
351
+ Foo * foo = std::construct_at(reinterpret_cast<Foo* >(buffer), 42, "hello"); // 等价于 new (buffer) Foo(42);
352
+ ...
353
+ std::destroy_at(foo); // 等价于 foo->~ Foo();
354
+ }
355
+ ```
356
+
357
+ > {{ icon.detail }} 在[内存模型专题](cpp_memory.md)中有进一步的详解。
You can’t perform that action at this time.
0 commit comments