Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report