Skip to content

Commit bf5635d

Browse files
a few changes to "Templates"
and a definition of "Library"
1 parent 2950a03 commit bf5635d

File tree

1 file changed

+84
-18
lines changed

1 file changed

+84
-18
lines changed

CppCoreGuidelines.md

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

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

55
Editors:
66

@@ -12708,6 +12708,13 @@ In C++, these requirements are expressed by compile-time predicates called conce
1270812708

1270912709
Templates can also be used for meta-programming; that is, programs that compose code at compile time.
1271012710

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+
1271112718
Template use rule summary:
1271212719

1271312720
* [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
1285612863
Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates)
1285712864
is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.
1285812865

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+
1285912874
##### Enforcement
1286012875

1286112876
* 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
1298312998
Concepts are, however, crucial in the thinking about generic programming and the basis of much work on future C++ libraries
1298412999
(standard and other).
1298513000

13001+
This section assumes concept support
13002+
1298613003
Concept use rule summary:
1298713004

1298813005
* [T.10: Specify concepts for all template arguments](#Rt-concepts)
@@ -13016,8 +13033,8 @@ Specifying concepts for template arguments is a powerful design tool.
1301613033
##### Example
1301713034

1301813035
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>
1302113038
Iter find(Iter b, Iter e, Val v)
1302213039
{
1302313040
// ...
@@ -13026,19 +13043,23 @@ Specifying concepts for template arguments is a powerful design tool.
1302613043
or equivalently and more succinctly:
1302713044

1302813045
template<Input_iterator Iter, typename Val>
13029-
requires Equality_comparable<Value_type<Iter>, Val>
13046+
// requires Equality_comparable<Value_type<Iter>, Val>
1303013047
Iter find(Iter b, Iter e, Val v)
1303113048
{
1303213049
// ...
1303313050
}
1303413051

1303513052
##### Note
1303613053

13037-
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:
1303813059

1303913060
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>
1304213063
Iter find(Iter b, Iter e, Val v)
1304313064
{
1304413065
// ...
@@ -13060,7 +13081,7 @@ Flag template type arguments without concepts
1306013081

1306113082
##### Reason
1306213083

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)
1306413085
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.
1306513086

1306613087
##### Note
@@ -13123,16 +13144,24 @@ Readability. Direct expression of an idea.
1312313144
To say "`T` is `Sortable`":
1312413145

1312513146
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
1312713148
void sort(T&); // that is Sortable"
1312813149

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
1313013151
void sort(T&); // which is Sortable"
1313113152

13132-
void sort(Sortable&); // Best: "The parameter is Sortable"
13153+
void sort(Sortable&); // Best (assuming language support for concepts): "The parameter is Sortable"
1313313154

1313413155
The shorter versions better match the way we speak. Note that many templates don't need to use the `template` keyword.
1313513156

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+
1313613165
##### Enforcement
1313713166

1313813167
* 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
1381213841

1381313842
## <a name="SS-temp-def"></a>T.def: Template definitions
1381413843

13815-
???
13844+
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.
1381613847

1381713848
### <a name="Rt-depend"></a>T.60: Minimize a template's context dependencies
1381813849

1381913850
##### Reason
1382013851

13821-
Eases understanding. Minimizes errors from unexpected dependencies. Eases tool creation.
13852+
Eases understanding.
13853+
Minimizes errors from unexpected dependencies.
13854+
Eases tool creation.
1382213855

1382313856
##### Example
1382413857

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
1382613865

1382713866
##### Note
1382813867

1382913868
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](#???)
1383013870

1383113871
##### Enforcement
1383213872

@@ -15098,9 +15138,31 @@ This section contains ideas about ???
1509815138

1509915139
### <a name="Ra-reuse"></a>A.2 Express potentially reusable parts as a library
1510015140

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+
1510215164

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
1510415166

1510515167
???
1510615168

@@ -15892,9 +15954,13 @@ These assertions is currently macros (yuck!) pending standard commission decisio
1589215954

1589315955
## <a name="SS-gsl-concepts"></a>GSL.concept: Concepts
1589415956

15895-
These concepts (type predicates) are borrowed from Andrew Sutton's Origin library, the Range proposal, and the ISO WG21 Palo Alto TR.
15957+
These concepts (type predicates) are borrowed from
15958+
Andrew Sutton's Origin library,
15959+
the Range proposal,
15960+
and the ISO WG21 Palo Alto TR.
1589615961
They are likely to be very similar to what will become part of the ISO C++ standard.
15897-
The notation is that of the ISO WG21 Concepts TS (???ref???).
15962+
The notation is that of the ISO WG21 [Concepts TS](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf).
15963+
Most of the concepts below are defined in [the Ranges TS](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf).
1589815964

1589915965
* `Range`
1590015966
* `String` // ???

0 commit comments

Comments
 (0)