Here, the std::span is temporary in the scope of the for loop, but not the objects it points to.
cppcheck error (file paths were removed):
error: Using pointer that is a temporary. [danglingTemporaryLifetime]
return selectedValue ? *selectedValue : 0;
^
note: Assigned to reference.
for (const int& value : getSpan(values))
^
note: Address of variable taken here.
selectedValue = &value;
^
note: Temporary created here.
for (const int& value : getSpan(values))
^
note: Using pointer that is a temporary.
return selectedValue ? *selectedValue : 0;
^
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:
It seems that saving the address of an element when iterating on a temporary std::span triggers this error.
Sample code:
Here, the std::span is temporary in the scope of the for loop, but not the objects it points to.
cppcheck error (file paths were removed):
Last edit: Cyphall 2025-07-12
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14014