From e63549f287e2aa2863eafb05ac633dd8e3e92df1 Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Fri, 6 May 2016 15:39:41 -0400 Subject: [PATCH] Improve F.22 --- CppCoreGuidelines.md | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index b21ee83..b5e6836 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1,6 +1,6 @@ # C++ Core Guidelines -April 23, 2016 +May 6, 2016 Editors: @@ -2592,6 +2592,11 @@ In some cases it may be useful to return a specific, user-defined `Value_or_erro ##### Reason +Readability: it makes the meaning of a plain pointer clear. +Enables significant tool support. + +##### Note + In traditional C and C++ code, plain `T*` is used for many weakly-related purposes, such as: * 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 * Identify an array with a length specified separately * Identify a location in an array +The makes it hard to understand what code does and is supposed to do. +It complicates checking and tool support. + ##### Example - void use(int* p, char* s, int* q) + void use(int* p, int nchar* s, int* q) { - // Bad: we don't know if p points to two elements; assume it does not or - // use span - *++p = 666; + p[n-1] = 666; // Bad: we don't know if p points to n elements; assume it does not or use span + + 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 - // Bad: we don't know if that s points to a zero-terminated array of char; - // assume it does not or use zstring - cout << s; + delete q; // Bad: we don't know if *q is allocated on the free store; assume it does not or use owner + } - // Bad: we don't know if *q is allocated on the free store; assume it does - // not or use owner - delete q; +better + + void use2(span p, zstring s, owner q) + { + p[p.size()-1] = 666; // OK, a range error can be caught + + cout << s; // OK + + delete q; // OK } ##### Note