Skip to content

Commit 0eda69d

Browse files
author
Colin Robertson
authored
Style fixes
Update style for Acrolinx and i18n, add comments, error message.
1 parent fbb20ea commit 0eda69d

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

docs/code-quality/c26402.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
---
22
title: C26402
3-
ms.date: 09/04/2019
3+
ms.date: 08/20/2020
44
ms.topic: "conceptual"
55
f1_keywords: ["C26402"]
66
helpviewer_keywords: ["C26402"]
77
ms.assetid: b9d3d398-697a-4a5d-8bfe-9c667dffb90b
88
---
99
# C26402 DONT_HEAP_ALLOCATE_MOVABLE_RESULT
1010

11-
To avoid confusion about whether a pointer owns an object, a function that returns a movable object should allocate it on the stack and return it by value instead of returning a heap-allocated object. If pointer semantics are required, then return a smart pointer instead of a raw pointer. See [C++ Core Guidelines R.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-ptr): *Warn if a function returns an object that was allocated within the function but has a move constructor. Suggest considering returning it by value instead.*
11+
> `Return a scoped object instead of a heap-allocated if it has a move constructor (r.3).`
12+
13+
## Remarks
14+
15+
To avoid confusion about whether a pointer owns an object, a function that returns a movable object should allocate it on the stack. It should then return the object by value instead of returning a heap-allocated object. If pointer semantics are required, return a smart pointer instead of a raw pointer. For more information, see [C++ Core Guidelines R.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-ptr): *Warn if a function returns an object that was allocated within the function but has a move constructor. Suggest considering returning it by value instead.*
16+
17+
## Example
18+
19+
This example shows a function, `bad_example`, that raises warning C26409. It also shows how function `good_example` doesn't cause this issue.
1220

13-
# Example
1421
```cpp
22+
// C26402.cpp
23+
1524
struct S
1625
{
17-
S() = default;
18-
S(S&& s) = default;
26+
S() = default;
27+
S(S&& s) = default;
1928
};
2029

21-
S* foo()
30+
S* bad_example()
2231
{
23-
S* s = new S(); // C26409, avoid explicitly calling new.
24-
// ...
25-
return s; // C26402
32+
S* s = new S(); // C26409, avoid explicitly calling new.
33+
// ...
34+
return s; // C26402
2635
}
2736

2837
// Prefer returning objects with move contructors by value instead of unnecessarily heap-allocating the object.
29-
S bar()
38+
S good_example() noexcept
3039
{
31-
S s;
32-
// ...
33-
return s;
40+
S s;
41+
// ...
42+
return s;
3443
}
3544
```

0 commit comments

Comments
 (0)