Skip to content

Commit e63549f

Browse files
Improve F.22
1 parent 7c9ec43 commit e63549f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

CppCoreGuidelines.md

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

3-
April 23, 2016
3+
May 6, 2016
44

55
Editors:
66

@@ -2592,6 +2592,11 @@ In some cases it may be useful to return a specific, user-defined `Value_or_erro
25922592

25932593
##### Reason
25942594

2595+
Readability: it makes the meaning of a plain pointer clear.
2596+
Enables significant tool support.
2597+
2598+
##### Note
2599+
25952600
In traditional C and C++ code, plain `T*` is used for many weakly-related purposes, such as:
25962601

25972602
* Identify a (single) object (not to be deleted by this function)
@@ -2601,21 +2606,29 @@ In traditional C and C++ code, plain `T*` is used for many weakly-related purpos
26012606
* Identify an array with a length specified separately
26022607
* Identify a location in an array
26032608

2609+
The makes it hard to understand what code does and is supposed to do.
2610+
It complicates checking and tool support.
2611+
26042612
##### Example
26052613

2606-
void use(int* p, char* s, int* q)
2614+
void use(int* p, int nchar* s, int* q)
26072615
{
2608-
// Bad: we don't know if p points to two elements; assume it does not or
2609-
// use span<int>
2610-
*++p = 666;
2616+
p[n-1] = 666; // Bad: we don't know if p points to n elements; assume it does not or use span<int>
2617+
2618+
cout << s; // Bad: we don't know if that s points to a zero-terminated array of char; // assume it does not or use zstring
26112619

2612-
// Bad: we don't know if that s points to a zero-terminated array of char;
2613-
// assume it does not or use zstring
2614-
cout << s;
2620+
delete q; // Bad: we don't know if *q is allocated on the free store; assume it does not or use owner
2621+
}
2622+
2623+
better
2624+
2625+
void use2(span<int> p, zstring s, owner<int*> q)
2626+
{
2627+
p[p.size()-1] = 666; // OK, a range error can be caught
2628+
2629+
cout << s; // OK
26152630

2616-
// Bad: we don't know if *q is allocated on the free store; assume it does
2617-
// not or use owner
2618-
delete q;
2631+
delete q; // OK
26192632
}
26202633

26212634
##### Note

0 commit comments

Comments
 (0)