From 78dd55be86cca8e51c09834dc0a4a637582f7421 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 16 Aug 2023 09:36:21 +0200 Subject: [PATCH] Coding Guidelines: Mention reserve()/resize() explicitly, with links --- CODING_GUIDELINES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODING_GUIDELINES.md b/CODING_GUIDELINES.md index 79e5623b10..64c2f8b1f1 100644 --- a/CODING_GUIDELINES.md +++ b/CODING_GUIDELINES.md @@ -227,8 +227,8 @@ The use of a container and its corresponding `at()` operator would have prevente The cost of using `at()` is negligible for most use cases, and can be avoided by using the `[]` operator in the rare case when the cost cannot be afforded. Note that several Linux distributions now build with `-Wp,-D_GLIBCXX_ASSERTIONS` enabled by default, which turns on cheap range checks for C++ arrays, vectors, and strings. -Regarding performance, it is advised to `reserve()` the needed size in advance when a rough estimate is known to avoid reallocations and copies. It usually triggers the allocation of enough memory to hold the requested number of items but does not increase the size of the container as reported by `size()`. -Resizing in advance is not advised, though, as it makes it harder to exactly know what is in the container in case of early returns or exceptions. +Regarding performance, it is advised to [`reserve()`](https://en.cppreference.com/w/cpp/container/vector/reserve) the needed size in advance when a rough estimate is known to avoid reallocations and copies. It usually triggers the allocation of enough memory to hold the requested number of items but does not increase the size of the container as reported by `size()`. +Calling [`resize()`](https://en.cppreference.com/w/cpp/container/vector/resize) in advance is not advised, though, as it makes it harder to exactly know what is in the container in case of early returns or exceptions. In C++11, move operators make it possible to cheaply get the contents of a container into a different variable if needed. -- 2.47.2