Warning C26418

Shared pointer parameter is not copied or moved. Use T* or T& instead.

C++ Core Guidelines: R.36: Take a const shared_ptr<widget>& parameter to express that it might retain a reference count to the object

If a shared pointer parameter is passed by value or by reference to a constant object, the function is expected to take control of the target object's lifetime without affecting the caller. The code should either copy or move the shared pointer parameter to another shared pointer object, or pass it along to other code by invoking functions that accept shared pointers. Otherwise, a plain pointer or reference may be feasible.

Remarks

  • This check recognizes std::shared_pointer and user-defined types that 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);

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

  • a public destructor that isn't deleted or defaulted. Empty destructors are still counted as user-defined.

Examples

unnecessary interface complication

template<class T>
std::string to_string(const std::shared_ptr<T> &e) // C26418, also C26415 SMART_PTR_NOT_NEEDED
{
    return !e ? null_string : e->to_string();
}

unnecessary interface complication - simplified

template<class T>
std::string to_string(const T *e)
{
    return !e ? null_string : e->to_string();
}