1
1
# <a name="main"></a>C++ Core Guidelines
2
2
3
- April 23 , 2016
3
+ May 6 , 2016
4
4
5
5
Editors:
6
6
@@ -2592,6 +2592,11 @@ In some cases it may be useful to return a specific, user-defined `Value_or_erro
2592
2592
2593
2593
##### Reason
2594
2594
2595
+ Readability: it makes the meaning of a plain pointer clear.
2596
+ Enables significant tool support.
2597
+
2598
+ ##### Note
2599
+
2595
2600
In traditional C and C++ code, plain `T*` is used for many weakly-related purposes, such as:
2596
2601
2597
2602
* 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
2601
2606
* Identify an array with a length specified separately
2602
2607
* Identify a location in an array
2603
2608
2609
+ The makes it hard to understand what code does and is supposed to do.
2610
+ It complicates checking and tool support.
2611
+
2604
2612
##### Example
2605
2613
2606
- void use(int* p, char * s, int* q)
2614
+ void use(int* p, int nchar * s, int* q)
2607
2615
{
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
2611
2619
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
2615
2630
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
2619
2632
}
2620
2633
2621
2634
##### Note
0 commit comments