From 9590bb94b10ee78439589b658c6fdfa6234bd6ed Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Fri, 26 Aug 2016 03:47:06 +0200 Subject: [PATCH 1/5] fix code examples --- CppCoreGuidelines.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 2f00f37..264d38b 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -587,7 +587,7 @@ You don't need to write error handlers for errors caught at compile time. This example is easily simplified // Int is an alias used for integers - static_assert(sizeof(Int) >= 4); // do: compile-time check + static_assert(sizeof(int) >= 4); // do: compile-time check ##### Example @@ -2609,6 +2609,7 @@ Unique owner types that are move-only and cheap-to-move, such as `unique_ptr`, c For example: + template void sink(std::unique_ptr p) { // use p ... possibly std::move(p) onward somewhere else } // p gets destroyed @@ -6610,7 +6611,7 @@ Capping an individual virtual function with `final` is error-prone as that `fina // ... - use(new Better_interface{}); + use(new Better_implementation{}); The problem is easy to see in a small example, but in a large hierarchy with many virtual functions, tools are required for reliably spotting such problems. Consistent use of `override` would catch this. @@ -7234,7 +7235,7 @@ Readability. Convention. Reusability. Support for generic code ##### Example - void cout_my_class(const my_class& c) // confusing, not conventional,not generic + void cout_my_class(const My_class& c) // confusing, not conventional,not generic { std::cout << /* class members here */; } @@ -15016,7 +15017,7 @@ This limits use and typically increases code size. }; List lst1; - List lst2; + List lst2; ??? @@ -15043,7 +15044,7 @@ This looks innocent enough, but ??? }; List lst1; - List lst2; + List lst2; ??? @@ -17464,7 +17465,7 @@ If code is using an unmodified standard library, then there are still workaround at(v, 0) = a[0]; // OK (alternative 2) v.at(0) = a[i]; // BAD - v.at(0) = a.at(i) // OK (alternative 1) + v.at(0) = a.at(i); // OK (alternative 1) v.at(0) = at(a, i); // OK (alternative 2) } From a361c37f5ee2af743a7cac6e8b42463e770ad86c Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Wed, 24 Aug 2016 12:44:47 +0200 Subject: [PATCH 2/5] unique funIds --- CppCoreGuidelines.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 264d38b..81b447c 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -17323,14 +17323,14 @@ Dynamic accesses into arrays are difficult for both tools and humans to validate // ALTERNATIVE A: Use a span // A1: Change parameter type to use span - void f(span a, int pos) + void f1(span a, int pos) { a[pos / 2] = 1; // OK a[pos - 1] = 2; // OK } // A2: Add local span and use that - void f(array arr, int pos) + void f2(array arr, int pos) { span a = {arr, pos} a[pos / 2] = 1; // OK @@ -17338,7 +17338,7 @@ Dynamic accesses into arrays are difficult for both tools and humans to validate } // ALTERNATIVE B: Use at() for access - void f(array a, int pos) + void f3(array a, int pos) { at(a, pos / 2) = 1; // OK at(a, pos - 1) = 2; // OK From 7c991f0e7ec0b09a2f645369a7ab90064551bacb Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Wed, 24 Aug 2016 12:46:38 +0200 Subject: [PATCH 3/5] fix varname --- CppCoreGuidelines.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 81b447c..4b56a65 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -16997,10 +16997,10 @@ Use of these casts can violate type safety and cause the program to access a var }; Derived1 d1; - Base* p = &d1; // ok, implicit conversion to pointer to Base is fine + Base* p1 = &d1; // ok, implicit conversion to pointer to Base is fine // BAD, tries to treat d1 as a Derived2, which it is not - Derived2* p2 = static_cast(p); + Derived2* p2 = static_cast(p1); // tries to access d1's nonexistent string member, instead sees arbitrary bytes near d1 cout << p2->get_s(); @@ -17136,7 +17136,7 @@ Note that a C-style `(T)expression` cast means to perform the first of the follo Base* p1 = &d1; // ok, implicit conversion to pointer to Base is fine // BAD, tries to treat d1 as a Derived2, which it is not - Derived2* p2 = (Derived2*)(p); + Derived2* p2 = (Derived2*)(p1); // tries to access d1's nonexistent string member, instead sees arbitrary bytes near d1 cout << p2->get_s(); From 25e3ec4652d901b7d9039df0a698e8f7bef1525d Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Wed, 24 Aug 2016 14:13:42 +0200 Subject: [PATCH 4/5] improve code example, use() function makes little sense with void results of f() and g() --- CppCoreGuidelines.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 4b56a65..d6fa3b4 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -15785,18 +15785,18 @@ Use the least-derived class that has the functionality you need. class Base { public: - void f(); - void g(); + Bar f(); + Bar g(); }; class Derived1 : public Base { public: - void h(); + Bar h(); }; class Derived2 : public Base { public: - void j(); + Bar j(); }; // bad, unless there is a specific reason for limiting to Derived1 objects only From 1c0e2b7d1184be2fb8645bb09e4c77b01fa3b886 Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Tue, 23 Aug 2016 10:36:43 +0200 Subject: [PATCH 5/5] fix invalid code --- CppCoreGuidelines.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index d6fa3b4..bd1419a 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -16138,7 +16138,7 @@ This enables the compiler to do an early consistency check. // foo.h: void foo(int); - int bar(long double); + int bar(long); int foobar(int); // foo.cpp: @@ -16152,7 +16152,7 @@ The errors will not be caught until link time for a program calling `bar` or `fo // foo.h: void foo(int); - int bar(long double); + int bar(long); int foobar(int); // foo.cpp: