You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+84-18Lines changed: 84 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# <a name="main"></a>C++ Core Guidelines
2
2
3
-
July 17, 2016
3
+
July 18, 2016
4
4
5
5
Editors:
6
6
@@ -12708,6 +12708,13 @@ In C++, these requirements are expressed by compile-time predicates called conce
12708
12708
12709
12709
Templates can also be used for meta-programming; that is, programs that compose code at compile time.
12710
12710
12711
+
A central notion in generic programming is "concepts"; that is, requirements on template arguments presented as compile-time predicates.
12712
+
"Concepts" are defined in an ISO Technical specification: [concepts](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf).
12713
+
A draft of a set of standard-library concepts can be found in another ISO TS: [ranges](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf)
12714
+
Currently (July 2016), concepts are supported only in GCC 6.1.
12715
+
Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only.
12716
+
If you use GCC 6.1, you can uncomment them.
12717
+
12711
12718
Template use rule summary:
12712
12719
12713
12720
* [T.1: Use templates to raise the level of abstraction of code](#Rt-raise)
@@ -12856,6 +12863,14 @@ We aim to minimize requirements on template arguments, but the absolutely minima
12856
12863
Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates)
12857
12864
is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.
12858
12865
12866
+
##### Note
12867
+
12868
+
The `requires` in the comments are uses of `concepts`.
12869
+
"Concepts" are defined in an ISO Technical specification: [concepts](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf).
12870
+
Currently (July 2016), concepts are supported only in GCC 6.1.
12871
+
Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only.
12872
+
If you use GCC 6.1, you can uncomment them.
12873
+
12859
12874
##### Enforcement
12860
12875
12861
12876
* Flag algorithms with "overly simple" requirements, such as direct use of specific operators without a concept.
@@ -12983,6 +12998,8 @@ It is an [ISO technical specification](#Ref-conceptsTS), but currently supported
12983
12998
Concepts are, however, crucial in the thinking about generic programming and the basis of much work on future C++ libraries
12984
12999
(standard and other).
12985
13000
13001
+
This section assumes concept support
13002
+
12986
13003
Concept use rule summary:
12987
13004
12988
13005
* [T.10: Specify concepts for all template arguments](#Rt-concepts)
@@ -13016,8 +13033,8 @@ Specifying concepts for template arguments is a powerful design tool.
13016
13033
##### Example
13017
13034
13018
13035
template<typename Iter, typename Val>
13019
-
requires Input_iterator<Iter>
13020
-
&& Equality_comparable<Value_type<Iter>, Val>
13036
+
// requires Input_iterator<Iter>
13037
+
// && Equality_comparable<Value_type<Iter>, Val>
13021
13038
Iter find(Iter b, Iter e, Val v)
13022
13039
{
13023
13040
// ...
@@ -13026,19 +13043,23 @@ Specifying concepts for template arguments is a powerful design tool.
Until your compilers support the concepts language feature, leave the concepts in comments:
13054
+
"Concepts" are defined in an ISO Technical specification: [concepts](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf).
13055
+
A draft of a set of standard-library concepts can be found in another ISO TS: [ranges](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf)
13056
+
Currently (July 2016), concepts are supported only in GCC 6.1.
13057
+
Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only.
13058
+
If you use GCC 6.1, you can uncomment them:
13038
13059
13039
13060
template<typename Iter, typename Val>
13040
-
// requires Input_iterator<Iter>
13041
-
// && Equality_comparable<Value_type<Iter>, Val>
13061
+
requires Input_iterator<Iter>
13062
+
&& Equality_comparable<Value_type<Iter>, Val>
13042
13063
Iter find(Iter b, Iter e, Val v)
13043
13064
{
13044
13065
// ...
@@ -13060,7 +13081,7 @@ Flag template type arguments without concepts
13060
13081
13061
13082
##### Reason
13062
13083
13063
-
"Standard" concepts (as provided by the GSL, the ISO concepts TS, Ranges TS, and hopefully soon the ISO standard itself)
13084
+
"Standard" concepts (as provided by the [GSL]#S-GSL), the ISO concepts TS, [Ranges TS](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf), and hopefully soon the ISO standard itself)
13064
13085
saves us the work of thinking up our own concepts, are better thought out than we can manage to do in a hurry, and improves interoperability.
13065
13086
13066
13087
##### Note
@@ -13123,16 +13144,24 @@ Readability. Direct expression of an idea.
13123
13144
To say "`T` is `Sortable`":
13124
13145
13125
13146
template<typename T> // Correct but verbose: "The parameter is
13126
-
requires Sortable<T> // of type T which is the name of a type
13147
+
// requires Sortable<T> // of type T which is the name of a type
13127
13148
void sort(T&); // that is Sortable"
13128
13149
13129
-
template<Sortable T> // Better: "The parameter is of type T
13150
+
template<Sortable T> // Better (assuming language support for concepts): "The parameter is of type T
13130
13151
void sort(T&); // which is Sortable"
13131
13152
13132
-
void sort(Sortable&); // Best: "The parameter is Sortable"
13153
+
void sort(Sortable&); // Best (assuming language support for concepts): "The parameter is Sortable"
13133
13154
13134
13155
The shorter versions better match the way we speak. Note that many templates don't need to use the `template` keyword.
13135
13156
13157
+
##### Note
13158
+
13159
+
"Concepts" are defined in an ISO Technical specification: [concepts](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf).
13160
+
A draft of a set of standard-library concepts can be found in another ISO TS: [ranges](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf)
13161
+
Currently (July 2016), concepts are supported only in GCC 6.1.
13162
+
Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only.
13163
+
If you use a compiler that supports concepts (e.g., GCC 6.1), you can remove the `//`.
13164
+
13136
13165
##### Enforcement
13137
13166
13138
13167
* Not feasible in the short term when people convert from the `<typename T>` and `<class T`> notation.
@@ -13812,21 +13841,32 @@ Type erasure incurs an extra level of indirection by hiding type information beh
A template definition (class or function) can contain arbitrary code, so only acomprehensive review of C++ programming techniques wouldcover this topic.
13845
+
However, this section focuses on what is specific to template implementation.
13846
+
In particular, it focuses on a template definition's dependence on its context.
13816
13847
13817
13848
### <a name="Rt-depend"></a>T.60: Minimize a template's context dependencies
13818
13849
13819
13850
##### Reason
13820
13851
13821
-
Eases understanding. Minimizes errors from unexpected dependencies. Eases tool creation.
13852
+
Eases understanding.
13853
+
Minimizes errors from unexpected dependencies.
13854
+
Eases tool creation.
13822
13855
13823
13856
##### Example
13824
13857
13825
-
???
13858
+
template<typename C>
13859
+
void sort(C& c)
13860
+
{
13861
+
std::sort(begin(c),end(c)); // necessary and useful dependency
13862
+
}
13863
+
13864
+
??? // potentially surprising dependency
13826
13865
13827
13866
##### Note
13828
13867
13829
13868
Having a template operate only on its arguments would be one way of reducing the number of dependencies to a minimum, but that would generally be unmanageable. For example, an algorithm usually uses other algorithms.
13869
+
See also [T69](#???)
13830
13870
13831
13871
##### Enforcement
13832
13872
@@ -15098,9 +15138,31 @@ This section contains ideas about ???
15098
15138
15099
15139
### <a name="Ra-reuse"></a>A.2 Express potentially reusable parts as a library
15100
15140
15101
-
???
15141
+
##### Reason
15142
+
15143
+
##### Note
15144
+
15145
+
A library is a collection of declarations and definitions maintained, documented, and shipped together.
15146
+
A library could be a set of headers (a "header only library") or a set of headers plus a set of object files.
15147
+
A library can be statically or dynamically linked into a program, or it may be `#included`
15148
+
15149
+
### <a name="Ra-lib"></a>A.4 There should be no cycles among libraries
15150
+
15151
+
##### Reason
15152
+
15153
+
A cycle implies complication of the build process.
15154
+
Cycles are hard to understand and may introcude indeterminism (unspecified behavior).
15155
+
15156
+
##### Note
15157
+
15158
+
A library can contain cyclic references in the definition of its components. For example:
15159
+
15160
+
???
15161
+
15162
+
However, a library should not depend on another that depends on it.
15163
+
15102
15164
15103
-
### <a name="Ra-lib"></a>A.3 Express potentially separately maintained parts as a library
15165
+
### <a name="Ra-dag"></a>A.3 Express potentially separately maintained parts as a library
15104
15166
15105
15167
???
15106
15168
@@ -15892,9 +15954,13 @@ These assertions is currently macros (yuck!) pending standard commission decisio
0 commit comments