error: Reference to values that may be invalid. [invalidContainerReference]
value->data = 5;
^
note: Assigned to reference.
std::unique_ptr<Value>& value = values.emplace_back(std::make_unique<Value>(i));
^
note: Accessing container.
std::unique_ptr<Value>& value = values.emplace_back(std::make_unique<Value>(i));
^
note: Assuming condition is true.
for (int i = 0; i < 5; i++)
^
note: After calling 'emplace_back', iterators or references to the container's data may be invalid .
std::unique_ptr<Value>& value = values.emplace_back(std::make_unique<Value>(i));
^
note: Reference to values that may be invalid.
value->data = 5;
^
Last edit: Cyphall 2025-07-12
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Using emplace_back on a vector may indeed invalidate references to existing objects, but not to the emplaced object itself.
Sample code:
cppcheck error (file paths were removed):
Last edit: Cyphall 2025-07-12
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14013