Following code gives the dangling pointer error.
std::vector<std::pair<std::string, int>> c;
for (auto& b : c) {
const auto& [s, i] = b;
std::string_view v = s.substr(i);
std::cout << v;
}
I think that b
holds the reference to the std::pair<std::string, int>
, so s
should be the reference to the string in pair object. Why does this creates the dangling pointer error? What am I missing here? godbolt link: https://godbolt.org/z/4zMvbr