From 8557755adb84a72d88a0078d7ebe349bc8e93145 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 24 Oct 2024 11:51:04 -0700 Subject: [PATCH] Replace R.5 exception and examples with moving a BigObject to the heap to save stack Closes #2221 --- CppCoreGuidelines.md | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 8487b2b2b..e93e8f8fd 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -9622,24 +9622,7 @@ Exception: Do not produce such a warning on a local `Unique_pointer` to an unbou ##### Exception -It is OK to create a local `const unique_ptr` to a heap-allocated buffer, as this is a valid way to represent a scoped dynamic array. - -##### Example - -A valid use case for a local `const unique_ptr` variable: - - int get_median_value(const std::list& integers) - { - const auto size = integers.size(); - - // OK: declaring a local unique_ptr. - const auto local_buffer = std::make_unique_for_overwrite(size); - - std::copy_n(begin(integers), size, local_buffer.get()); - std::nth_element(local_buffer.get(), local_buffer.get() + size/2, local_buffer.get() + size); - - return local_buffer[size/2]; - } +If your stack space is limited, it is OK to create a local `const unique_ptr` to store the object on the heap instead of the stack. ### R.6: Avoid non-`const` global variables