Skip to content

Commit 0004b6e

Browse files
authored
Update c26820.md
Updated examples to better reflect common use cases
1 parent c85dd76 commit 0004b6e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

docs/code-quality/c26820.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ This check covers non-obvious and easy-to-miss behavior when assigning a referen
3131
This sample shows a variable definition that makes a potentially expensive copy when assigned a reference:
3232

3333
```cpp
34-
MyClass& ref = ...;
35-
auto var = ref; // C26820 (`var` takes a copy of the object referred to by `ref`)
34+
const Object& MyClass::getRef() { ... }
35+
...
36+
auto ref = myclass.getRef(); // C26820 (`ref` takes a copy of the returned object)
3637
```
3738
3839
To resolve this issue, declare the variable by using `const auto&` instead:
3940
4041
```cpp
41-
MyClass& ref = ...;
42-
const auto& var = ref; // OK
42+
const Object& MyClass::getRef() { ... }
43+
...
44+
const auto& ref = myclass.getRef(); // OK
4345
```

0 commit comments

Comments
 (0)