Warning C26410
The parameter 'parameter' is a reference to const unique pointer, use
const T*
orconst T&
instead (r.32)
Generally, references to const unique pointer are meaningless. They can safely be replaced by a raw reference or a pointer. This warning enforces C++ Core Guidelines rule R.32.
Remarks
Unique pointer checks have rather broad criteria to identify smart pointers. The C++ Core Guidelines rule R.31: If you have non-std smart pointers, follow the basic pattern from std describes the unique pointer and shared pointer concepts. The heuristic is simple, but may lead to surprises: a smart pointer type is any type that defines either
operator->
oroperator*
. A copy-able type (shared pointer) must have either a public copy constructor or an overloaded assignment operator that deals with a non-Rvalue reference parameter.Template code may produce noisy warnings. Keep in mind that templates can be instantiated with various type parameters with different levels of indirection, including references. Some warnings may not be obvious and fixes may require some rework of templates (for example, explicit removal of reference indirection). If template code is intentionally generic, the warning can be suppressed.
Code analysis name: NO_REF_TO_CONST_UNIQUE_PTR
Example
Unnecessary reference:
std::vector<std::unique_ptr<Tree>> roots = GetRoots();
std::for_each(
roots.begin(),
roots.end(),
[](const auto &root) { Rebalance(root.get()); }); // C26410