You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/code-quality/c26820.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ helpviewer_keywords: ["C26820"]
7
7
---
8
8
# C26820
9
9
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).
11
11
12
12
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.
13
13
@@ -31,13 +31,15 @@ This check covers non-obvious and easy-to-miss behavior when assigning a referen
31
31
This sample shows a variable definition that makes a potentially expensive copy when assigned a reference:
32
32
33
33
```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)
36
37
```
37
38
38
39
To resolve this issue, declare the variable by using `const auto&` instead:
This following procedures show you how to create the sample for [Walkthrough: Analyze C/C++ code for defects](../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md). The procedures create:
13
+
The following procedures show you how to create the sample for [Walkthrough: Analyze C/C++ code for defects](../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md). The procedures create:
13
14
14
-
- A Visual Studio solution named CppDemo.
15
+
- A Visual Studio solution named *CppDemo*.
15
16
16
-
- A static library project named CodeDefects.
17
+
- A static library project named *CodeDefects*.
17
18
18
-
- A static library project named Annotations.
19
+
- A static library project named *Annotations*.
19
20
20
21
The procedures also provide the code for the header and *.cpp* files for the static libraries.
21
22
22
23
## Create the CppDemo solution and the CodeDefects project
23
24
25
+
::: moniker range=">=vs-2019"
26
+
24
27
1. Open Visual Studio and select **Create a new project**
25
28
26
-
1. Change language filter to **C++**
29
+
1. In the **Create a new project** dialog, change the language filter to **C++**.
30
+
31
+
1. Select **Windows Desktop Wizard** and choose the **Next** button.
32
+
33
+
1. On the **Configure your new project** page, in the **Project name** text box, enter *CodeDefects*.
34
+
35
+
1. In the **Solution name** text box, enter *CppDemo*.
36
+
37
+
1. Choose **Create**.
38
+
39
+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
40
+
41
+
1. Under **Additional options**, select **Empty project**.
42
+
43
+
1. Choose **OK** to create the solution and project.
44
+
45
+
::: moniker-end
46
+
47
+
::: moniker range="vs-2017"
48
+
49
+
1. Open Visual Studio. On the menu bar, choose **File** > **New** > **Project**.
50
+
51
+
1. In the **New Project** dialog, select **Visual C++** > **Windows Desktop**.
52
+
53
+
1. Select **Windows Desktop Wizard**.
54
+
55
+
1. In the **Name** text box, enter *CodeDefects*.
56
+
57
+
1. In the **Solution name** text box, enter *CppDemo*.
27
58
28
-
1.Select**Empty Project** and click **Next**
59
+
1.Choose**OK**.
29
60
30
-
1. In the **Project Name**text box, type**CodeDefects**
61
+
1. In the **Windows Desktop Project**dialog, change the **Application type** to **Static Library (.lib)**.
31
62
32
-
1.In the **Solution name** text box, type**CppDemo**
// Stops copying on the domain and user separator ('\')
101
161
break;
102
162
}
103
163
domain[len] = g_userAccount[len];
104
164
}
105
-
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
165
+
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
106
166
{
107
167
// '\' was not found. Invalid domain\user string.
108
168
delete[] domain;
109
169
return false;
110
170
}
111
171
else
112
172
{
113
-
domain[len] = '\0';
173
+
domain[len] = L'\0';
114
174
}
115
175
// Process domain string
116
176
bool result = CheckDomain(domain);
@@ -133,31 +193,77 @@ The procedures also provide the code for the header and *.cpp* files for the sta
133
193
}
134
194
```
135
195
136
-
1.Click the **File**menu, and then click**Save All**.
196
+
1.On the menu bar, choose **File**>**Save All**.
137
197
138
198
## Add the Annotations project and configure it as a static library
139
199
140
-
1. In Solution Explorer, click **CppDemo**, point to **Add**, and then click **New Project**.
200
+
::: moniker range=">=vs-2019"
201
+
202
+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
203
+
204
+
1. In the **Add a new project** dialog box, select **Windows Desktop Wizard**, and then choose the **Next** button.
205
+
206
+
1. On the **Configure your new project** page, in the **Project name** text box, enter *Annotations*, and then choose **Create**.
207
+
208
+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
209
+
210
+
1. Under **Additional options**, select **Empty project**.
211
+
212
+
1. Choose **OK** to create the project.
213
+
214
+
::: moniker-end
215
+
216
+
::: moniker range="vs-2017"
217
+
218
+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
219
+
220
+
1. In the **Add New Project** dialog, select **Visual C++** > **Windows Desktop**.
221
+
222
+
1. Select **Windows Desktop Wizard**.
223
+
224
+
1. In the **Name** text box, enter *Annotations*, and then choose **OK**.
225
+
226
+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
227
+
228
+
1. Under **Additional options**, select **Empty project**.
141
229
142
-
1.In the **Add a new project**dialog box, Change language filter to **C++** and select **Empty Project** then click **Next**.
230
+
1.Choose **OK** to create the project.
143
231
144
-
1. In the **Project name** text box, type **Annotations**, and then click **Create**.
232
+
::: moniker-end
145
233
146
-
1. In Solution Explorer, right-click **Annotations** and then click **Properties**.
234
+
::: moniker range="vs-2015"
147
235
148
-
1.Expand **Configuration Properties**and then click **General**.
236
+
1.In Solution Explorer, right-click **CppDemo**to open the context menu. Choose **Add** > **New Project**.
149
237
150
-
1. In the **General**list, change**Configuration Type**, to and then click **Static library (.lib)**.
238
+
1. In the **Add New Project**dialog, select**Visual C++** > **Win32**.
151
239
152
-
1. In the **Advanced** list, select the text in the column next to **Target File extension**, and then type **.lib**.
240
+
1. Select **Win32 Console Application**.
241
+
242
+
1. In the **Name** text box, enter *Annotations*.
243
+
244
+
1. Choose **OK**.
245
+
246
+
1. In the **Win32 Application Wizard** dialog, choose the **Next** button.
247
+
248
+
1. Change the **Application type** to **Static library**.
249
+
250
+
1. Under **Additional options**, unselect **Precompiled header**.
251
+
252
+
1. Choose **Finish** to create the project.
253
+
254
+
::: moniker-end
153
255
154
256
## Add the header file and source file to the Annotations project
155
257
156
-
1. In Solution Explorer, expand **Annotations**, right-click **Header Files**, click **Add**, and then click **New Item**.
258
+
1. In Solution Explorer, expand **Annotations**.
259
+
260
+
1. Right-click to open the context menu for **Header Files** under **Annotations**. Choose **Add** > **New Item**.
157
261
158
-
1. In the **Add New Item** dialog box, click**Header File (.h)**.
262
+
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select**Header File (.h)**.
159
263
160
-
1. In the **Name** box, type **annotations.h** and then click **Add**.
264
+
1. In the **Name** edit box, enter *annotations.h*, and then choose the **Add** button.
265
+
266
+
1. In the edit window for *annotations.h*, select and delete the contents.
161
267
162
268
1. Copy the following code and paste it into the *annotations.h* file in the editor.
163
269
@@ -176,16 +282,23 @@ The procedures also provide the code for the header and *.cpp* files for the sta
176
282
_Ret_maybenull_ LinkedList* AllocateNode();
177
283
```
178
284
179
-
1. In Solution Explorer, right-click **Source Files**, point to **New**, and then click**New Item**.
285
+
1. In Solution Explorer, right-click to open the context menu for **Source Files** under **Annotations**. Choose **Add** >**New Item**.
180
286
181
-
1. In the **Add New Item** dialog box, click**Code** and then click **C++ File (.cpp)**
287
+
1. In the **Add New Item** dialog box, select**C++ File (.cpp)**.
182
288
183
-
1. In the **Name** box, type **annotations.cpp** and then click **Add**.
289
+
1. In the **Name**edit box, enter *annotations.cpp*, and then choose the **Add** button.
184
290
185
291
1. Copy the following code and paste it into the *annotations.cpp* file in the editor.
186
292
187
293
```cpp
188
294
#include"annotations.h"
295
+
#include<malloc.h>
296
+
297
+
_Ret_maybenull_ LinkedList* AllocateNode()
298
+
{
299
+
LinkedList* result = static_cast<LinkedList*>(malloc(sizeof(LinkedList)));
300
+
return result;
301
+
}
189
302
190
303
LinkedList* AddTail(LinkedList* node, int value)
191
304
{
@@ -205,6 +318,13 @@ The procedures also provide the code for the header and *.cpp* files for the sta
205
318
}
206
319
```
207
320
208
-
1.Click the **File**menu, and then click**Save All**.
321
+
1.On the menu bar, choose **File**>**Save All**.
209
322
210
323
The solution is now complete and should build without errors.
324
+
325
+
::: moniker range="vs-2017"
326
+
327
+
> [!NOTE]
328
+
> In Visual Studio 2017, you may see a spurious warning `E1097 unknown attribute "no_init_all"` in the IntelliSense engine. You can safely ignore this warning.
0 commit comments