From: Remi Gacogne Date: Wed, 16 Aug 2023 07:36:21 +0000 (+0200) Subject: Coding Guidelines: Mention reserve()/resize() explicitly, with links X-Git-Tag: rec-5.0.0-alpha1~44^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78dd55be86cca8e51c09834dc0a4a637582f7421;p=thirdparty%2Fpdns.git Coding Guidelines: Mention reserve()/resize() explicitly, with links --- 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.