From 7e5019378ba3b90496ccf2f8e15710051f5ed271 Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Mon, 1 Jan 2018 10:40:45 -0500 Subject: [PATCH] Fix #493 array myth Added comment about performance to SL.con.1 --- CppCoreGuidelines.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 8020056..3b66251 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1,6 +1,6 @@ # C++ Core Guidelines -December 26, 2017 +Janualy 1, 2018 Editors: @@ -18558,6 +18558,13 @@ For a variable-length array, use `std::vector`, which additionally can change it Use `gsl::span` for non-owning references into a container. +##### Note + +Comparing the performance of a fixed-sized array allocated on the stack against a `vector` with its elements on the free store is bogus. +You could just as well compare a `std::array` on the stack against the result of a `malloc()` accessed through a pointer. +For most code, even the difference between stack allocation and free-store allocation doesn't matter, but the convenieance and safety of `vector` does. +People working with code for which that difference matters are quite capable of choosing between `array` and `vector`. + ##### Enforcement * Flag declaration of a C array inside a function or class that also declares an STL container (to avoid excessive noisy warnings on legacy non-STL code). To fix: At least change the C array to a `std::array`.