In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "when C++ uses smart pointers as parameters". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
R.30: use smart pointers as parameters only when explicit lifecycle semantics are included
Reason (reason)
If a function only needs a part itself, it is wrong to accept a smart pointer as an argument. It should be able to accept all part objects, not just an object whose lifecycle is managed in a specific way. Functions that do not need to manage the life cycle should use raw pointers and references.
Example, bad (negative example)
/ / callee
Void f (shared_ptr& w)
{
/ /...
Use (* w); / / only use of w-- the lifetime is not used at all
/ /...
}
/ / caller
Shared_ptr my_widget = / *... * /
F (my_widget)
Widget stack_widget
F (stack_widget); / / errorExample, good (example) / / callee
Void f (widget& w)
{
/ /...
Use (w)
/ /...
}
/ / caller
Shared_ptr my_widget = / *... * /
F (* my_widget)
Widget stack_widget
F (stack_widget); / / ok-now this worksEnforcement (implementation recommendation)
(Simple) Warn if a function takes a parameter of a smart pointer type (that overloads operator- > or operator*) that is copyable but the function only calls any of: operator*, operator- > or get () Suggest using a T * or T & instead.
(simple) if a function uses a parameter of the copyable (overloaded operator-> and operator *) smart pointer type but only calls the operators *,->, or get (), issue a warning and recommend using T* or T&.
Flag a parameter of a smart pointer type (a type that overloads operator- > or operator*) that is copyable/movable but never copied/moved from in the function body, and that is never modified, and that is not passed along to another function that could do so. That means the ownership semantics are not used. Suggest using a T * or T & instead.
Tags define parameters of copiable / removable smart pointer types (overloaded with operators-> and operator *), but copy and move functions are never used in the function body, and pointers are never modified or handed over to a function that will do so. That means that ownership semantics are not used at all. It is recommended to use T* or T&.
This is the end of the content of "when C++ uses smart pointers as parameters". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.