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 does C++ realize the intelligent pointer outside the standard library?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how C++ can achieve smart pointers outside the standard library". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ can achieve smart pointers outside the standard library.

R.31: if you need to implement smart pointers other than the standard library, follow the basic pattern in the standard library

Reason (reason)

The guidelines in later chapters also apply to other types of third-party and custom smart pointers, which are effective in finding general smart pointer errors that can cause performance and correctness problems. What you need is a rule that works for all smart pointers.

Any type that overloads unary * and-> (including major templates and specializations) can be considered a smart pointer.

If it is copiable, it is considered a shared pointer with a reference count.

If it is not copiable, it is considered an exclusive unique_ptr.

Example (sample)

/ / use Boost's intrusive_ptr

# include

Void f (boost::intrusive_ptr p) / / error under rule 'sharedptrparam'

{

P-> foo ()

}

/ / use Microsoft's CComPtr

# include

Void f (CComPtr p) / / error under rule 'sharedptrparam'

{

P-> foo ()

}

In both cases, a mistake was made in the sharedptrParam guidelines: P is a shared pointer, but nothing about sharing is used here. And passing smart pointers by value is a default permission; this function should accept smart pointers only when participating in the lifecycle management of widget. Other cases: if functions are allowed to be null, they should accept widget*, otherwise they should accept widget&. These smart pointers match the concept of Shared_pointer, so the rules recommended by these guidelines can also be applied to them immediately.

At this point, I believe you have a deeper understanding of "how C++ can achieve smart pointers outside the standard library". 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