mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
F.21: fix variable name in example and text (#1981)
This commit is contained in:
@@ -3251,14 +3251,14 @@ In such cases, passing the object by reference [`T&`](#Rf-inout) is usually the
|
|||||||
Explicitly passing an in-out parameter back out again as a return value is often not necessary.
|
Explicitly passing an in-out parameter back out again as a return value is often not necessary.
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
istream& operator>>(istream& is, string& s); // much like std::operator>>()
|
istream& operator>>(istream& in, string& s); // much like std::operator>>()
|
||||||
|
|
||||||
for (string s; cin >> s; ) {
|
for (string s; in >> s; ) {
|
||||||
// do something with line
|
// do something with line
|
||||||
}
|
}
|
||||||
|
|
||||||
Here, both `s` and `cin` are used as in-out parameters.
|
Here, both `s` and `in` are used as in-out parameters.
|
||||||
We pass `cin` by (non-`const`) reference to be able to manipulate its state.
|
We pass `in` by (non-`const`) reference to be able to manipulate its state.
|
||||||
We pass `s` to avoid repeated allocations.
|
We pass `s` to avoid repeated allocations.
|
||||||
By reusing `s` (passed by reference), we allocate new memory only when we need to expand `s`'s capacity.
|
By reusing `s` (passed by reference), we allocate new memory only when we need to expand `s`'s capacity.
|
||||||
This technique is sometimes called the "caller-allocated out" pattern and is particularly useful for types,
|
This technique is sometimes called the "caller-allocated out" pattern and is particularly useful for types,
|
||||||
@@ -3266,11 +3266,11 @@ such as `string` and `vector`, that needs to do free store allocations.
|
|||||||
|
|
||||||
To compare, if we passed out all values as return values, we would something like this:
|
To compare, if we passed out all values as return values, we would something like this:
|
||||||
|
|
||||||
pair<istream&, string> get_string(istream& is) // not recommended
|
pair<istream&, string> get_string(istream& in) // not recommended
|
||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
is >> s;
|
in >> s;
|
||||||
return {is, move(s)};
|
return {in, move(s)};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto p = get_string(cin); p.first; ) {
|
for (auto p = get_string(cin); p.first; ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user