C++: Update documentation on 'cpp/iterator-to-expired-container'.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-04-30 16:32:32 +01:00
parent 3eddd3114f
commit 708d12624f
2 changed files with 12 additions and 0 deletions

View File

@@ -30,6 +30,12 @@ This is because the temporary container is not bound to a rvalue reference.
</p>
<sample src="IteratorToExpiredContainerExtendedLifetime.cpp" />
<p>
To fix <code>lifetime_of_temp_not_extended</code> consider rewriting the code so that the temporary's lifetime is extended.
In <code>fixed_lifetime_of_temp_not_extended</code> the lifetime of the temporary object has been extended by storing it in an rvalue reference.
</p>
<sample src="IteratorToExpiredContainerExtendedLifetime-fixed.cpp" />
</example>
<references>

View File

@@ -0,0 +1,6 @@
void fixed_lifetime_of_temp_not_extended() {
auto&& v = get_vector();
for(auto x : log_and_return_argument(v)) {
use(x); // GOOD: The lifetime of the container returned by `get_vector()` has been extended to the lifetime of `v`.
}
}