Skip to content

Commit b5e9dc1

Browse files
authored
Merge pull request #2801 from dmitrykobets/patch-1
Update c26820.md
2 parents 598bd8b + 5b54d7f commit b5e9dc1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

docs/code-quality/c26820.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ helpviewer_keywords: ["C26820"]
77
---
88
# C26820
99

10-
> Assigning by value when a const-reference would suffice, use const T& instead (p.9).
10+
> Assigning by value when a const-reference would suffice, use const auto& instead (p.9).
1111
1212
For more information, see [P.9: Don't waste time or space](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#p9-dont-waste-time-or-space) in the C++ Core Guidelines.
1313

@@ -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)