1
1
# <a name="main"></a>C++ Core Guidelines
2
2
3
- July 18 , 2016
3
+ July 19 , 2016
4
4
5
5
Editors:
6
6
@@ -12795,7 +12795,7 @@ Metaprogramming rule summary:
12795
12795
12796
12796
Other template rules summary:
12797
12797
12798
- * [T.140: Name all nontrivial operations](#Rt-name)
12798
+ * [T.140: Name all operations with potential for reuse ](#Rt-name)
12799
12799
* [T.141: Use an unnamed lambda if you need a simple function object in one place only](#Rt-lambda)
12800
12800
* [T.142: Use template variables to simplify notation](#Rt-var)
12801
12801
* [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.
14568
14568
14569
14569
## <a name="SS-temp-other"></a>Other template rules
14570
14570
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
14572
14572
14573
14573
##### Reason
14574
14574
14575
14575
Documentation, readability, opportunity for reuse.
14576
14576
14577
14577
##### Example
14578
14578
14579
- ???
14579
+ struct Rec {
14580
+ string name;
14581
+ string addr;
14582
+ int id; // unique identifier
14583
+ };
14580
14584
14581
- ##### Example, good
14585
+ bool same(const Rec& a, const Rec& b)
14586
+ {
14587
+ return a.id==b.id;
14588
+ }
14582
14589
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
+ );
14584
14620
14585
14621
##### Note
14586
14622
14587
14623
whether functions, lambdas, or operators.
14588
14624
14589
- ##### Exceptions
14625
+ ##### Exception
14590
14626
14591
14627
* Lambdas logically used only locally, such as an argument to `for_each` and similar control flow algorithms.
14592
14628
* Lambdas as [initializers](#???)
@@ -14605,8 +14641,11 @@ That makes the code concise and gives better locality than alternatives.
14605
14641
14606
14642
auto earlyUsersEnd = std::remove_if(users.begin(), users.end(),
14607
14643
[](const User &a) { return a.id > 100; });
14644
+
14645
+
14646
+ ##### Exception
14608
14647
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.
14610
14649
14611
14650
##### Enforcement
14612
14651
0 commit comments