Skip to content

Commit

Permalink
fix(A8-4-9): Split A8-4-8 and A8-4-9
Browse files Browse the repository at this point in the history
  • Loading branch information
sbmueller committed Sep 4, 2023
1 parent ce86c72 commit 9b55501
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
68 changes: 10 additions & 58 deletions src/A8-4-8.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ struct B

struct BB
{
B GetB() const& { return obj; }
B&& GetB() && { return std::move(obj); }
B GetB() const& { return obj; }
B&& GetB() && { return std::move(obj); }

B obj;
B obj;

};

// Non-compliant: returns a dangling reference
BB&& MakeBb1()
{
return std::move(BB());
return std::move(BB());
}

// Compliant: uses compiler copy-ellision
BB MakeBb2()
{
return BB();
return BB();

}

int main()
{
BB x = MakeBb2();
BB x = MakeBb2();

auto cpd = x.GetB();
// copied value
auto mvd = MakeBb2().GetB(); // moved value
auto cpd = x.GetB();
// copied value
auto mvd = MakeBb2().GetB(); // moved value

return 0;
return 0;

}

Expand All @@ -67,51 +67,3 @@ return 0;
C++ Core Guidelines [11]: F.20: For ”out” output values, prefer return values to
output parameters.
Rule A8-4-9 (required, design, automated) ”in-out”
parameters declared as T & shall be modified.
## Rationale
An ”in-out” parameter is declared with a type of reference to non-const. This means
that a fully constructed object is passed into the function that can be read as well as
modified.
Note: Completely replacing the passed in object without reading any data from it
would make it an ”out” parameter instead and is not considered compliant with this
rule, also see rule: A8-4-8
## Example
```cpp
// $Id: A8-4-9.cpp 306178 2018-02-01 15:52:25Z christof.meerwald $
#include <cstdint>
#include <numeric>
#include <string>
#include <vector>
// Non-Compliant: does not modify the "in-out" parameter
int32_t Sum(std::vector<int32_t> &v)
{
return std::accumulate(v.begin(), v.end(), 0);
}
// Compliant: Modifying "in-out" parameter
void AppendNewline(std::string &s)
{
s += ’\n’;
}
// Non-Compliant: Replacing parameter value
void GetFileExtension(std::string &ext)
{
ext = ".cpp";
}
```

## See also

C++ Core Guidelines [11]: F.17: For ”in-out” parameters, pass by reference to
non-const
JSF December 2005 [8]: AV Rule 117.2: An object should be passed as T& if
the function may change the value of the object.
49 changes: 49 additions & 0 deletions src/A8-4-9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
> **Rule A8-4-9 (required, design, automated)**
>
> ”in-out” parameters declared as T & shall be modified.
## Rationale

An ”in-out” parameter is declared with a type of reference to non-const. This means
that a fully constructed object is passed into the function that can be read as well as
modified.
Note: Completely replacing the passed in object without reading any data from it
would make it an ”out” parameter instead and is not considered compliant with this
rule, also see rule: A8-4-8

## Example

```cpp
// $Id: A8-4-9.cpp 306178 2018-02-01 15:52:25Z christof.meerwald $

#include <cstdint>
#include <numeric>
#include <string>
#include <vector>

// Non-Compliant: does not modify the "in-out" parameter
int32_t Sum(std::vector<int32_t> &v)
{
return std::accumulate(v.begin(), v.end(), 0);
}

// Compliant: Modifying "in-out" parameter
void AppendNewline(std::string &s)
{
s += ’\n’;
}

// Non-Compliant: Replacing parameter value
void GetFileExtension(std::string &ext)
{
ext = ".cpp";
}

```
## See also
C++ Core Guidelines [11]: F.17: For ”in-out” parameters, pass by reference to
non-const
JSF December 2005 [8]: AV Rule 117.2: An object should be passed as T& if
the function may change the value of the object.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
- [A8-4-6](./A8-4-6.md)
- [A8-4-7](./A8-4-7.md)
- [A8-4-8](./A8-4-8.md)
- [A8-4-9](./A8-4-9.md)
- [A8-4-10](./A8-4-10.md)
- [A8-4-11](./A8-4-11.md)
- [A8-4-12](./A8-4-12.md)
Expand Down

0 comments on commit 9b55501

Please sign in to comment.