Skip to content

Commit 9a63e9b

Browse files
authored
Merge pull request MicrosoftDocs#2703 from MicrosoftDocs/master
2/25/2020 AM Publish
2 parents ee38541 + 114c9fc commit 9a63e9b

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎docs/build/cmakesettings-reference.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `configurations` array contains all the configurations for a CMake project.
2424

2525
A `configuration` has these properties:
2626

27-
- `addressSDanitizerEnabled`: if `true` compiles the program with Address Sanitizer (Experimental on Windows). On Linux, compile with -fno-omit-frame-pointer and compiler optimization level -Os or -Oo for best results.
27+
- `addressSanitizerEnabled`: if `true` compiles the program with Address Sanitizer (Experimental on Windows). On Linux, compile with -fno-omit-frame-pointer and compiler optimization level -Os or -Oo for best results.
2828
- `addressSanitizerRuntimeFlags`: runtime flags passed to AddressSanitizer via the ASAN_OPTIONS environment variable. Format: flag1=value:flag2=value2.
2929
- `buildCommandArgs`: specifies native build switches passed to CMake after --build --. For example, passing -v when using the Ninja generator forces Ninja to output command lines. See [Ninja command line arguments](#ninja) for more information on Ninja commands.
3030
- `buildRoot`: specifies the directory in which CMake generates build scripts for the chosen generator. Maps to **-DCMAKE_BINARY_DIR** switch and specifies where *CMakeCache.txt* will be created. If the folder does not exist, it is created. Supported macros include `${workspaceRoot}`, `${workspaceHash}`, `${projectFile}`, `${projectDir}`, `${thisFile}`, `${thisFileDir}`, `${name}`, `${generator}`, `${env.VARIABLE}`.

‎docs/code-quality/c26817.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: C26817
3+
description: "Reference for Microsoft C++ Code Analysis warning C26817 in Visual Studio."
4+
ms.date: 02/24/2020
5+
ms.topic: "reference"
6+
f1_keywords: ["C26817"]
7+
helpviewer_keywords: ["C26817"]
8+
---
9+
# C26817
10+
11+
> Potentially expensive copy of variable *name* in range-for loop. Consider making it a const reference (es.71).
12+
13+
For more information, see [ES.71 notes](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#note-217) in the C++ Core Guidelines.
14+
15+
## Example
16+
17+
If a range-for loop variable isn't explicitly marked as a reference, it gets a copy of each element iterated over:
18+
19+
```cpp
20+
#include <vector>
21+
22+
class MyComplexType {
23+
int native_array[1000];
24+
// ...
25+
};
26+
27+
void expensive_function(std::vector<MyComplexType>& complex_vector_ref)
28+
{
29+
for (auto item: complex_vector_ref)
30+
{
31+
// At each iteration, item gets a copy of the next element
32+
// ...
33+
}
34+
for (MyComplexType item: complex_vector_ref)
35+
{
36+
// It happens whether you use the auto keyword or the type name
37+
// ...
38+
}
39+
}
40+
```
41+
42+
This behavior is fine for scalars (pointers, arithmetic types, and so on), but for larger types, the copying may become expensive.
43+
44+
## Solution
45+
46+
To fix this issue, if the loop variable isn't mutated anywhere in the loop, make it a const reference:
47+
48+
```cpp
49+
#include <vector>
50+
51+
class MyComplexType {
52+
int native_array[1000];
53+
// ...
54+
};
55+
56+
void less_expensive_function(std::vector<MyComplexType>& complex_vector_ref)
57+
{
58+
for (const auto& item: complex_vector_ref)
59+
{
60+
// item no longer gets a copy of each iterated element
61+
// ...
62+
}
63+
for (const MyComplexType& item: complex_vector_ref)
64+
{
65+
// item no longer gets a copy of each iterated element
66+
// ...
67+
}
68+
}
69+
```
70+
71+
The **const** keyword makes the loop variable immutable. Use of a non-const reference may cause potentially unwanted side effects in the original container elements. If you need to modify only the local loop variable, the potentially expensive copying is unavoidable.

‎docs/code-quality/toc.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@
211211
href: c26815.md
212212
- name: C26816
213213
href: c26816.md
214+
- name: C26817
215+
href: c26817.md
214216
- name: C/C++ warnings
215217
items:
216218
- name: Overview

0 commit comments

Comments
 (0)