Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How C++ uses make_shared to build shared_ptr-managed objects

Shulou Source: shulou.com Published: 2022-06-02 05:17:27 09月21日 Update

This article mainly explains how C++ uses make_shared to build shared_ptr-managed objects, which interested friends may wish to have a look at. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how C++ uses make_shared to build shared_ptr-managed objects.

Using make_shared to build objects managed by shared_ptr

Reason (reason)

Make_shared gives a more concise statement of the construction. It also gives an opportunity to eliminate a separate allocation for the reference counts, by placing the shared_ptr's use counts next to its object.

Make_shared provides a more concise expression for construction actions. Because it puts the count of shared_ptr after the object, using it also provides an opportunity to reduce another increase in count.

Example (sample)

Void test () {

/ / OK: but repetitive; and separate allocations for the Bar and shared_ptr's use count

Shared_ptr p {new Bar {7}}

Auto Q = make_shared (7); / / Better: no repetition of Bar; one object

} Enforcement (implementation recommendations)

Flag the repetitive usage of template specialization list

Prompt for reuse of template specialization parameter list

Flag variables declared to be shared_ptr

Prompts you to use shared_ptr to define variables directly.

At this point, I believe you have a deeper understanding of "how C++ uses make_shared to build shared_ptr-managed objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Object management caching content situation learning hint practicality depth conciseness interest action reason parameter variable practicality practicality suggestion simple operation method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi NVidia Shulou Technology macOS