Skip to content

Commit 1b026e5

Browse files
Slight cleanup in profiles section
1 parent cb5bab9 commit 1b026e5

File tree

1 file changed

+93
-24
lines changed

1 file changed

+93
-24
lines changed

CppCoreGuidelines.md

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,7 +5757,7 @@ There are two major uses for hierarchies, often named implementation inheritance
57575757

57585758
Class hierarchy rule summary:
57595759

5760-
* [C.120: Use class hierarchies to represent concepts with inherent hierarchical structure](#Rh-domain)
5760+
* [C.120: Use class hierarchies to represent concepts with inherent hierarchical structure (only)](#Rh-domain)
57615761
* [C.121: If a base class is used as an interface, make it a pure abstract class](#Rh-abstract)
57625762
* [C.122: Use abstract classes as interfaces when complete separation of interface and implementation is needed](#Rh-separation)
57635763

@@ -15371,6 +15371,7 @@ It is more likely to be stable, well-maintained, and widely available than your
1537115371

1537215372
C arrays are less safe, and have no advantages over `array` and `vector`.
1537315373
For a fixed-length array, use `std::array`, which does not degenerate to a pointer when passed to a function and does know its size.
15374+
Also, like a built-in array, a stack-allocated `std::array` keeps its elements on the stack.
1537415375
For a variable-length array, use `std::vector`, which additionally can change its size and handles memory allocation.
1537515376

1537615377
##### Example
@@ -15497,19 +15498,33 @@ However, a library should not depend on another that depends on it.
1549715498

1549815499
???
1549915500

15500-
# <a name="S-not"></a>Non-Rules and myths
15501+
# <a name="S-not"></a>NR: Non-Rules and myths
1550115502

1550215503
This section contains rules and guidelines that are popular somewhere, but that we deliberately don't recommend.
1550315504
In the context of the styles of programming we recommend and support with the guidelines, these "non-rules" would do harm.
1550415505

1550515506
Non-rule summary:
1550615507

15507-
* all declarations on top of function
15508+
* [NR.1: All declarations shuld be at the of a function](#Rnr-top)
1550815509
* single-return rule
1550915510
* no exceptions
1551015511
* one class per source file
1551115512
* two-phase initialization
1551215513
* goto exit
15514+
* make all data members `protected`
15515+
* ???
15516+
15517+
### <a name="Rnr-top"></a>NR.1: All declarations shuld be at the of a function
15518+
15519+
##### Reason
15520+
15521+
This rule is a legacy of old programming languages that didn't allow initialization of variables and constants after a statement.
15522+
This leads to longer programs and more errors caused by uninitialized and wrongly initialized variables.
15523+
15524+
##### Alternative
15525+
15526+
Instead, [Always initialize an object](#Res-always)
15527+
and [ES.21: Don't introduce a variable (or constant) before you need to use it](#Res-introduce).
1551315528

1551415529
# <a name="S-references"></a>RF: References
1551515530

@@ -15605,9 +15620,12 @@ Reference sections:
1560515620

1560615621
## <a name="SS-Cplusplus"></a>RF.C++: C++ Programming (C++11/C++14)
1560715622

15608-
* TC++PL4
15609-
* Tour++
15610-
* Programming: Principles and Practice using C++
15623+
* [TC++PL4](http://www.stroustrup.com/4th.html):
15624+
A thorough description of the C++ language and standard libraries for experienced programmers.
15625+
* [Tour++](http://www.stroustrup.com/Tour.html):
15626+
An overview of the C++ language and standard libraries for experienced programmers.
15627+
* [Programming: Principles and Practice using C++](http://www.stroustrup.com/programming.html):
15628+
A textbook for beginners and relative novices.
1561115629

1561215630
## <a name="SS-web"></a>RF.web: Websites
1561315631

@@ -15617,6 +15635,8 @@ Reference sections:
1561715635
* [Boost](http://www.boost.org)<a name="Boost"></a>
1561815636
* [Adobe open source](http://www.adobe.com/open-source.html)
1561915637
* [Poco libraries](http://pocoproject.org/)
15638+
* Sutter's Mill?
15639+
* ???
1562015640

1562115641
## <a name="SS-vid"></a>RS.video: Videos about "modern C++"
1562215642

@@ -15647,7 +15667,9 @@ Thanks to the many people who contributed rules, suggestions, supporting informa
1564715667
* Zhuang, Jiangang (Jeff)
1564815668
* Sergey Zubkov
1564915669

15650-
# <a name="S-profile"></a>Profiles
15670+
and see the contributor list on the github.
15671+
15672+
# <a name="S-profile"></a>PRO: Profiles
1565115673

1565215674
A "profile" is a set of deterministic and portably enforceable subset rules (i.e., restrictions) that are designed to achieve a specific guarantee. "Deterministic" means they require only local analysis and could be implemented in a compiler (though they don't need to be). "Portably enforceable" means they are like language rules, so programmers can count on enforcement tools giving the same answer for the same code.
1565315675

@@ -15659,22 +15681,46 @@ Profiles summary:
1565915681
* [Pro.bounds: Bounds safety](#SS-bounds)
1566015682
* [Pro.lifetime: Lifetime safety](#SS-lifetime)
1566115683

15662-
## <a name="SS-type"></a>Type safety profile
15684+
In the future, we expect to define many more profiles and add morechecks to existing profiles.
15685+
Candidates include:
1566315686

15664-
This profile makes it easier to construct code that uses types correctly and avoids inadvertent type punning. It does so by focusing on removing the primary sources of type violations, including unsafe uses of casts and unions.
15687+
* narrowing arithmetic promotions/conversions (likely part of a separate safe-arithmetic profile)
15688+
* arithmetic cast from negative floating point to unsigned integral type (ditto)
15689+
* selected undefined behavior: ??? start with Gaby's UB list
15690+
* selected unspecified behavior: ??? a portability concern?
15691+
* `const` violations
1566515692

15666-
For the purposes of this section, type-safety is defined to be the property that a program does not use a variable as a type it is not. Memory accessed as a type `T` should not be valid memory that actually contains an object of an unrelated type `U`. (Note that the safety is intended to be complete when combined also with [Bounds safety](#SS-bounds) and [Lifetime safety](#SS-lifetime).)
15693+
To suppress enforcement of a profile check, place a `suppress` annotation on a language contract. For example:
1566715694

15668-
The following are under consideration but not yet in the rules below, and may be better in other profiles:
15695+
[[suppress(bounds)]] char* raw_find(char* p, int n, char x) // find x in p[0]..p[n-1]
15696+
{
15697+
// ...
15698+
}
1566915699

15670-
* narrowing arithmetic promotions/conversions (likely part of a separate safe-arithmetic profile)
15671-
* arithmetic cast from negative floating point to unsigned integral type (ditto)
15672-
* selected undefined behavior: ??? this is a big bucket, start with Gaby's UB list
15673-
* selected unspecified behavior: ??? would this really be about safety, or more a portability concern?
15674-
* constness violations? if we rely on it for safety
15700+
Now `raw_find()` can scramble memory to its heart's content.
15701+
Obviously, suppression should be very rare.
15702+
15703+
## <a name="SS-type"></a>PRO.safety: Type safety profile
15704+
15705+
This profile makes it easier to construct code that uses types correctly and avoids inadvertent type punning.
15706+
It does so by focusing on removing the primary sources of type violations, including unsafe uses of casts and unions.
15707+
15708+
For the purposes of this section,
15709+
type-safety is defined to be the property that a variable is not used in a way that doesn't obey the rules for the type of its definition.
15710+
Memory accessed as a type `T` should not be valid memory that actually contains an object of an unrelated type `U`.
15711+
Note that the safety is intended to be complete when combined also with [Bounds safety](#SS-bounds) and [Lifetime safety](#SS-lifetime).
1567515712

1567615713
An implementation of this profile shall recognize the following patterns in source code as non-conforming and issue a diagnostic.
1567715714

15715+
Type safety profile summary:
15716+
15717+
* [Type.1: Don't use `reinterpret_cast`](#Pro-type-reinterpretcast)
15718+
* [Type.2: Don't use `static_cast` downcasts. Use `dynamic_cast` instead](#Pro-type-downcast)
15719+
* [Type.3: Don't use `const_cast` to cast away `const` (i.e., at all)](#Pro-type-constcast)
15720+
* [Type.4: Don't use C-style `(T)expression` casts that would perform a `static_cast` downcast, `const_cast`, or `reinterpret_cast`](#Pro-type-cstylecast)
15721+
* [Type.5: Don't use a variable before it has been initialized](#Pro-type-init)
15722+
* [Type.6: Always initialize a member variable](#Pro-type-memberinit)
15723+
1567815724
### <a name="Pro-type-reinterpretcast"></a>Type.1: Don't use `reinterpret_cast`.
1567915725

1568015726
##### Reason
@@ -16188,6 +16234,8 @@ If code is using an unmodified standard library, then there are still workaround
1618816234

1618916235
## <a name="SS-lifetime"></a>Lifetime safety profile
1619016236

16237+
???
16238+
1619116239
# <a name="S-gsl"></a>GSL: Guideline support library
1619216240

1619316241
The GSL is a small library of facilities designed to support this set of guidelines.
@@ -16199,7 +16247,20 @@ The GSL is header only, and can be found at [GSL: Guideline support library](htt
1619916247
The support library facilities are designed to be extremely lightweight (zero-overhead) so that they impose no overhead compared to using conventional alternatives.
1620016248
Where desirable, they can be "instrumented" with additional functionality (e.g., checks) for tasks such as debugging.
1620116249

16202-
These Guidelines assume a `variant` type, but this is not currently in GSL because the design is being actively refined in the standards committee.
16250+
These Guidelines assume a `variant` type, but this is not currently in GSL.
16251+
Eventually, use [the one voted into C++17](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0088r3.html).
16252+
16253+
Summary of GSL components:
16254+
16255+
* [GSL.view: Views](#SS-views)
16256+
* [GSL.owner](#Ownership pointers)
16257+
* [GSL.assert: Assertions](#SS-assertions)
16258+
* [GSL.util: Utilities](#SS-utilities)
16259+
* [GSL.concept: Concepts](#SS-gsl-concepts)
16260+
16261+
We plan for a "ISO C++ standard style" semi-formal specification of the GSL.
16262+
16263+
We rely on the ISO C++ standard library and hope for parts of the GSL to be absorbed into the standard library.
1620316264

1620416265
## <a name="SS-views"></a>GSL.view: Views
1620516266

@@ -16238,7 +16299,7 @@ If something is not supposed to be `nullptr`, say so:
1623816299
* `not_null<T>` // `T` is usually a pointer type (e.g., `not_null<int*>` and `not_null<owner<Foo*>>`) that may not be `nullptr`.
1623916300
`T` can be any type for which `==nullptr` is meaningful.
1624016301

16241-
* `span<T>` // \[`p`:`p+n`), constructor from `{p, q}` and `{p, n}`; `T` is the pointer type
16302+
* `span<T>` // `[`p`:`p+n`), constructor from `{p, q}` and `{p, n}`; `T` is the pointer type
1624216303
* `span_p<T>` // `{p, predicate}` \[`p`:`q`) where `q` is the first element for which `predicate(*p)` is true
1624316304
* `string_span` // `span<char>`
1624416305
* `cstring_span` // `span<const char>`
@@ -16273,7 +16334,10 @@ Use `not_null<zstring>` for C-style strings that cannot be `nullptr`. ??? Do we
1627316334
// `Expect` in under control of some options (enforcement, error message, alternatives to terminate)
1627416335
* `Ensures` // postcondition assertion. Currently placed in function bodies. Later, should be moved to declarations.
1627516336

16276-
These assertions is currently macros (yuck!) pending standard commission decisions on contracts and assertion syntax.
16337+
These assertions is currently macros (yuck!) and must appear in function definitions (only)
16338+
pending standard commission decisions on contracts and assertion syntax.
16339+
See [the contract proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0380r1.pdf) uses the attribute syntax,
16340+
for example, `Expects(p!=nullptr)` will become`[[expects: p!=nullptr]]`.
1627716341

1627816342
## <a name="SS-utilities"></a>GSL.util: Utilities
1627916343

@@ -16297,16 +16361,16 @@ Most of the concepts below are defined in [the Ranges TS](http://www.open-std.or
1629716361
* `String` // ???
1629816362
* `Number` // ???
1629916363
* `Sortable`
16300-
* `Pointer` // A type with `*`, `->`, `==`, and default construction (default construction is assumed to set the singular "null" value) [see smartptrconcepts](#Rr-smartptrconcepts)
16301-
* `Unique_ptr` // A type that matches `Pointer`, has move (not copy), and matches the Lifetime profile criteria for a `unique` owner type [see smartptrconcepts](#Rr-smartptrconcepts)
16302-
* `Shared_ptr` // A type that matches `Pointer`, has copy, and matches the Lifetime profile criteria for a `shared` owner type [see smartptrconcepts](#Rr-smartptrconcepts)
16364+
* `Pointer` // A type with `*`, `->`, `==`, and default construction (default construction is assumed to set the singular "null" value); see [smart pointers](#Rr-smartptrconcepts)
16365+
* `Unique_ptr` // A type that matches `Pointer`, has move (not copy), and matches the Lifetime profile criteria for a `unique` owner type; see [smart pointers](#Rr-smartptrconcepts)
16366+
* `Shared_ptr` // A type that matches `Pointer`, has copy, and matches the Lifetime profile criteria for a `shared` owner type; see [smart pointers](#Rr-smartptrconcepts)
1630316367
* `EqualityComparable` // ???Must we suffer CaMelcAse???
1630416368
* `Convertible`
1630516369
* `Common`
1630616370
* `Boolean`
1630716371
* `Integral`
1630816372
* `SignedIntegral`
16309-
* `SemiRegular`
16373+
* `SemiRegular` // ??? Copyable?
1631016374
* `Regular`
1631116375
* `TotallyOrdered`
1631216376
* `Function`
@@ -16345,7 +16409,12 @@ Naming and layout rules:
1634516409
* [NL.25: Don't use `void` as an argument type](#Rl-void)
1634616410

1634716411
Most of these rules are aesthetic and programmers hold strong opinions.
16348-
IDEs also tend to have defaults and a range of alternatives. These rules are suggested defaults to follow unless you have reasons not to.
16412+
IDEs also tend to have defaults and a range of alternatives.
16413+
These rules are suggested defaults to follow unless you have reasons not to.
16414+
16415+
We have had comments to the effect that naming and layout are so personal and/or arbitrary that we should not try to "legislate" them.
16416+
We are not "legislating" (see the previous paragraph).
16417+
However, we have had many requests for a set of naming and layout conventions to use when there are no external constraints.
1634916418

1635016419
More specific and detailed rules are easier to enforce.
1635116420

0 commit comments

Comments
 (0)