Skip to content

Commit 95acdad

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#2431 from MicrosoftDocs/master637346684842127587
Repo sync for protected CLA branch
2 parents 4750f5e + 1df40e1 commit 95acdad

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

docs/code-quality/c26460.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,34 @@ ms.date: 03/22/2018
44
ms.topic: reference
55
f1_keywords: ["C26460"]
66
helpviewer_keywords: ["C26460"]
7+
description: CppCoreCheck rule that enforces C++ Core Guidelines Con.3
78
---
89
# C26460 USE_CONST_REFERENCE_ARGUMENTS
10+
The reference argument '%argument%' for function '%function%' can be marked as `const` (con.3).
911

10-
The reference argument '%argument%' for function '%function%' can be marked as `const`. See [C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref).
12+
Passing an object by reference indicates that the function has the potential modify the object. If that is not the intent of the function, it is better to mark the argument as a const reference.
13+
14+
## See also
15+
[C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref).
16+
17+
## Example
18+
```cpp
19+
struct MyStruct
20+
{
21+
void MemberFn1() const;
22+
void MemberFn2();
23+
};
24+
25+
26+
void Function1_Helper(const MyStruct&);
27+
void Function1(MyStruct& myStruct) // C26460, see comments below.
28+
{
29+
myStruct.MemberFn1(); // The member function is marked as const
30+
Function1_Helper(myStruct); // Function1_Helper takes a const reference
31+
}
32+
33+
void Function2(MyStruct& myStruct)
34+
{
35+
myStruct.MemberFn2(); // MemberFn2 is non-const and has the potential to modify data
36+
}
37+
```

docs/code-quality/c26461.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,40 @@ ms.date: 03/22/2018
44
ms.topic: reference
55
f1_keywords: ["C26461"]
66
helpviewer_keywords: ["C26461"]
7+
description: CppCoreCheck rule that enforces C++ Core Guidelines con.3
78
---
89
# C26461 USE_CONST_POINTER_ARGUMENTS:
910

10-
The pointer argument '%argument%' for function '%function%' can be marked as a pointer to `const`. See [C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref).
11+
The pointer argument '%argument%' for function '%function%' can be marked as a pointer to `const` (con.3).
12+
13+
A function with a `T*` argument has the potential to modify the value of the object. If that is not the intent of the function, it is better to make the pointer a `const T*` instead.
14+
15+
## See also
16+
[C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref).
17+
18+
## Example
19+
```cpp
20+
struct MyStruct
21+
{
22+
void MemberFn1() const;
23+
void MemberFn2();
24+
};
25+
26+
void Function1_Helper(const MyStruct* myStruct);
27+
void Function1(MyStruct* myStruct) // C26461, neither of the operations on myStruct would modify the pointer's value.
28+
{
29+
if (!myStruct)
30+
return;
31+
32+
myStruct->MemberFn1(); // The member function is const
33+
Function1_Helper(myStruct); // Function1_Helper takes a const
34+
}
35+
36+
void Function2(MyStruct* myStruct)
37+
{
38+
if (!myStruct)
39+
return;
40+
41+
myStruct->MemberFn2(); // The member function is non-const, so no C26461 will be issued
42+
}
43+
```

0 commit comments

Comments
 (0)