Compilling the code below in MSVS with c++ core guidlines enabled produces warning:
C26486 LIFETIMES_FUNCTION_PRECONDITION_VIOLATION - Link to relevant Microsoft Docs
using vec_pair = std::vector<std::pair<const std::string_view, const std::string_view>>;
void foo(const vec_pair& vec_pair) noexcept
{
for (auto [first, second] : vec_pair) //<---- Warning here
{
// do stuff
}
}
int main()
{
const vec_pair my_vec_pair { {
{"yes", "no"},
{"what", "why"},
{"salt", "pepper"},
} };
foo(my_vec_pair);
return 0;
}
Description is:
Don't pass a pointer that may be invalid to a function. Parameter 0 '$S1' in call to '<move><std::pair<std::basic_string_view<char,std::char_traits<char> > const ,std::basic_string_view<char,std::char_traits<char> > const > & __ptr64>' may be invalid (lifetime.3).
Changing std::string_view to std::string does not help. Nor does checking for nullptr both before and after the for loop.
How should I deal with this?