mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 04:44:34 +03:00
Replace R.5 exception and examples with moving a BigObject to the heap to save stack
Closes #2221
This commit is contained in:
@@ -9622,24 +9622,7 @@ Exception: Do not produce such a warning on a local `Unique_pointer` to an unbou
|
|||||||
|
|
||||||
##### Exception
|
##### Exception
|
||||||
|
|
||||||
It is OK to create a local `const unique_ptr<T[]>` to a heap-allocated buffer, as this is a valid way to represent a scoped dynamic array.
|
If your stack space is limited, it is OK to create a local `const unique_ptr<BigObject>` to store the object on the heap instead of the stack.
|
||||||
|
|
||||||
##### Example
|
|
||||||
|
|
||||||
A valid use case for a local `const unique_ptr<T[]>` variable:
|
|
||||||
|
|
||||||
int get_median_value(const std::list<int>& integers)
|
|
||||||
{
|
|
||||||
const auto size = integers.size();
|
|
||||||
|
|
||||||
// OK: declaring a local unique_ptr<T[]>.
|
|
||||||
const auto local_buffer = std::make_unique_for_overwrite<int[]>(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];
|
|
||||||
}
|
|
||||||
|
|
||||||
### <a name="Rr-global"></a>R.6: Avoid non-`const` global variables
|
### <a name="Rr-global"></a>R.6: Avoid non-`const` global variables
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user