I want to store a pointer to a vector element, selected in a range-based for loop, all that in a static context.
However, CLion gives me the warning Address of local variable may escape the function.
Here is an example:
struct TestStruct
{
static std::vector<std::string> _values;
static const std::string* _selectedValue;
static void selectValue()
{
for (const std::string& value : _values)
{
if (value == "toto")
{
_selectedValue = &value; // The warning is here, on the "&value" part.
}
}
}
};
When removing the three static keywords however, the warning goes away.
When I run the static and non-static versions, it work as expected in both cases, as the for loop uses references to elements.
Why is this warning present in the static version?
Is there something special about static variables lifetime that could make this not work?