Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.85 KB

c26416.md

File metadata and controls

49 lines (35 loc) · 1.85 KB
description title ms.date ms.topic f1_keywords helpviewer_keywords ms.assetid
Learn more about: C26416 NO_RVALUE_REF_SHARED_PTR
C26416
11/15/2017
conceptual
C26416
C26416
f158207b-45cf-44cf-8e4b-b5b75b56ea0e

C26416 NO_RVALUE_REF_SHARED_PTR

Shared pointer parameter is passed by rvalue reference. Pass by value instead.

C++ Core Guidelines: R.34: Take a shared_ptr<widget> parameter to express that a function is part owner

Passing a shared pointer by rvalue reference is usually unnecessary. Unless it is an implementation of move semantics for a shared pointer type itself, shared pointer objects can be safely passed by value. Using rvalue reference may be also an indication that unique pointer is more appropriate since it clearly transfers unique ownership from caller to callee.

Remarks

  • This check recognizes std::shared_pointer and user-defined types which are likely to behave like shared pointers. The following traits are expected for user-defined shared pointers:

  • overloaded dereference or member access operators (public and non-deleted);

  • copy constructor or copy assignment operator (public and non-deleted);

  • public destructor which is neither deleted nor defaulted. Empty destructors are still counted as user-defined.

Examples

questionable constructor optimization

action::action(std::shared_ptr<transaction> &&t) noexcept // C26416
    : transaction_(std::move(t))
{}

action::action(std::shared_ptr<transaction> &t) noexcept  // also C26417 LVALUE_REF_SHARED_PTR
    : transaction_(t)
{}

questionable constructor optimization - simplified

action::action(std::shared_ptr<transaction> t) noexcept
    : transaction_(std::move(t))
{}