Skip to content

Commit 25deeba

Browse files
T.140
1 parent 6d1de28 commit 25deeba

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

CppCoreGuidelines.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# <a name="main"></a>C++ Core Guidelines
22

3-
July 18, 2016
3+
July 19, 2016
44

55
Editors:
66

@@ -12795,7 +12795,7 @@ Metaprogramming rule summary:
1279512795

1279612796
Other template rules summary:
1279712797

12798-
* [T.140: Name all nontrivial operations](#Rt-name)
12798+
* [T.140: Name all operations with potential for reuse](#Rt-name)
1279912799
* [T.141: Use an unnamed lambda if you need a simple function object in one place only](#Rt-lambda)
1280012800
* [T.142: Use template variables to simplify notation](#Rt-var)
1280112801
* [T.143: Don't write unintentionally nongeneric code](#Rt-nongeneric)
@@ -14568,25 +14568,61 @@ Write your own "advanced TMP support" only if you really have to.
1456814568

1456914569
## <a name="SS-temp-other"></a>Other template rules
1457014570

14571-
### <a name="Rt-name"></a>T.140: Name all nontrivial operations
14571+
### <a name="Rt-name"></a>T.140: Name all operations with potential for reuse
1457214572

1457314573
##### Reason
1457414574

1457514575
Documentation, readability, opportunity for reuse.
1457614576

1457714577
##### Example
1457814578

14579-
???
14579+
struct Rec {
14580+
string name;
14581+
string addr;
14582+
int id; // unique identifier
14583+
};
1458014584

14581-
##### Example, good
14585+
bool same(const Rec& a, const Rec& b)
14586+
{
14587+
return a.id==b.id;
14588+
}
1458214589

14583-
???
14590+
vector<Rec*> find_id(const string& name); // find all records for "name"
14591+
14592+
auto x = find_if(vr.begin(),vr.end(),
14593+
[&](Rec& r) {
14594+
int (r.name.size()!=n.size()) return false; // name to compare to is in n
14595+
for (int i=0; i<r.name.size(); ++i) if (tolower(r.name[i])!=tolower(n[i])) return false;
14596+
return true;
14597+
}
14598+
);
14599+
14600+
There is a useful function lurking here (case insensitive string comparison), as there often is when lambda arguments get large.
14601+
14602+
bool compare_insensitive(const string& a, const string& b)
14603+
{
14604+
int (a.size()!=b.size()) return false;
14605+
for (int i=0; i<a.size(); ++i) if (tolower(a[i])!=tolower(b[i])) return false;
14606+
return true;
14607+
}
14608+
14609+
auto x = find_if(vr.begin(),vr.end(),
14610+
[&](Rec& r) { compare_insensitive(r.name, n); }
14611+
);
14612+
14613+
Or maybe (if you prefer to avoid the implicit name binding to n):
14614+
14615+
auto cmp_to_n = [&n](const string& a) { return compare_insensitive(a, n); };
14616+
14617+
auto x = find_if(vr.begin(),vr.end(),
14618+
[](const Rec& r) { return cmp_to_n(r.name); }
14619+
);
1458414620

1458514621
##### Note
1458614622

1458714623
whether functions, lambdas, or operators.
1458814624

14589-
##### Exceptions
14625+
##### Exception
1459014626

1459114627
* Lambdas logically used only locally, such as an argument to `for_each` and similar control flow algorithms.
1459214628
* Lambdas as [initializers](#???)
@@ -14605,8 +14641,11 @@ That makes the code concise and gives better locality than alternatives.
1460514641

1460614642
auto earlyUsersEnd = std::remove_if(users.begin(), users.end(),
1460714643
[](const User &a) { return a.id > 100; });
14644+
14645+
14646+
##### Exception
1460814647

14609-
**Exception**: Naming a lambda can be useful for clarity even if it is used only once
14648+
Naming a lambda can be useful for clarity even if it is used only once.
1461014649

1461114650
##### Enforcement
1461214651

0 commit comments

Comments
 (0)