std::vector<std::string> getData() { return {"OK?", "other", "ok"}; }
for (auto c : getData()[0]) {
std::cout << c << '\n';
}
Iterating over a reference to an rvalue was an error before C++23, but since C++23 it no longer is.
Thanks for making the approporiate fix in clang. However, there is still a warning as if we don't have the fix.
<source>:13:17: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
13 | for (auto c : getData()[0]) {
| ^~~~~~~~~
1 warning generated.
ASM generation compiler returned: 0
<source>:13:17: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
13 | for (auto c : getData()[0]) {
| ^~~~~~~~~
So, this warning should go away with C++23. Not sure how easy it is to fix it, though, as this is a special behavior just in the range-based for loop.
Consider (for the complete example, see: https://www.godbolt.org/z/oajGrd56M):
Iterating over a reference to an rvalue was an error before C++23, but since C++23 it no longer is.
Thanks for making the approporiate fix in clang. However, there is still a warning as if we don't have the fix.
So, this warning should go away with C++23. Not sure how easy it is to fix it, though, as this is a special behavior just in the range-based for loop.