Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

When does C++ use smart pointers as parameters?

Shulou Source: shulou.com Published: 2022-06-01 19:25:56 11月15日 Update

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!

Tags: Pointers parameters intelligence functions operators just period suggestion copy life time Category never content object situation more knowledge type semantics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno Docker MariaDB Redmi