Skip to content

Commit c9741f7

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#2381 from JordanMaples/patch-8
Add C++ Core Guidelines link to C26411
2 parents 0918ffe + 5709b4d commit c9741f7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/code-quality/c26411.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
22
title: C26411
3-
ms.date: 11/15/2017
3+
ms.date: 08/19/2020
44
ms.topic: "conceptual"
55
f1_keywords: ["C26411"]
66
helpviewer_keywords: ["C26411"]
77
ms.assetid: 5134e51e-8b92-4ee7-94c3-022e318a0e24
88
---
99
# C26411 NO_REF_TO_UNIQUE_PTR
1010

11-
Passing a unique pointer by reference assumes that its resource may be released or transferred inside of a target function. If function uses its parameter only to access the resource, it is safe to pass a raw pointer or a reference.
11+
When you pass a unique pointer to a function by reference, it implies that its resource may be released or transferred inside the function. If the function uses its parameter only to access the resource, it's safe to pass a raw pointer or a reference. For additional information, see [C++ Core Guidelines rule R.33](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r33-take-a-unique_ptrwidget-parameter-to-express-that-a-function-reseats-thewidget): *Take a unique_ptr\<widget\>& parameter to express that a function reseats the widget*.
1212

1313
## Remarks
1414

15-
- The limitations from the warning [C26410](C26410.md) are applicable here as well.
15+
- The limitations from the warning [C26410](C26410.md) are also applicable here.
1616

17-
- The heuristic to detect "release" or "reset" access to the unique pointer is rather naive: we only detect calls to assignment operators and functions named "reset" (case-insensitive). Obviously, this detection doesnt cover all possible cases of smart pointer modifications (for example, std::swap, or any special non-const function in a custom smart pointer). It is expected that this warning will produce many false positives on custom types, as well as in some scenarios dealing with standard unique pointers. The heuristic will be improved as we implement more checks focused on smart pointers.
17+
- The heuristic to detect `release` or `reset` access to the unique pointer is naive. We only detect calls to assignment operators and to functions named `reset` (case-insensitive). Obviously, this detection doesn't cover all possible cases of smart pointer modifications. (For example, it doesn't detect `std::swap`, or any special non-**`const`** function in a custom smart pointer). We expect this warning may produce many false positives on custom types, and in some scenarios dealing with standard unique pointers. We expect to improve the heuristic as we implement more checks focused on smart pointers.
1818

19-
- The fact that smart pointers are often templates brings an interesting limitation related to the fact that the compiler is not required to process template code in templates if it's not used. In some minimal code bases that have limited use of smart pointer interfaces, the checker may produce unexpected results due to its inability to properly identify semantics of the template type (because some important functions may never be used). For the standard `unique_pointer`, this limitation is mitigated by recognizing the types name. This may be extended in future to cover more well-known smart pointers.
19+
- The fact that smart pointers are often templates brings an interesting limitation. The compiler isn't required to process template code in templates if it's not used. In code that makes limited use of smart pointer interfaces, the checker may produce unexpected results. The checker can't properly identify semantics of the template type, because some functions may never get used. For the standard `std::unique_ptr`, this limitation is mitigated by recognizing the type's name. This may be extended in future to cover more well-known smart pointers.
2020

21-
- Lambda expressions with implicit capture-by-reference may lead to surprising warnings about references to unique pointers. Currently all captured reference parameters in lambdas are reported, regardless of whether they are reset or not. The heuristic here will be extended to correlate lambda fields with lambda parameters in a future release.
21+
- Lambda expressions that do implicit capture-by-reference may lead to surprising warnings about references to unique pointers. Currently, all captured reference parameters in lambdas are reported, regardless of whether they're reset or not. A future release may extend the heuristic to correlate lambda fields and lambda parameters.
2222

2323
## Example: Unnecessary reference
2424

0 commit comments

Comments
 (0)