I used the following syntactic sugar:
for (auto& numberString: {"one", "two", "three", "four"}) { /* ... */}
Is this valid code? AFAIK, based on this question, this should be illegal, yet the code runs as expected. I don't think my understanding is correct on the matter.
As far as I know, only literals should not have memory addresses, yet the linked question is talking about temporaries and r-values.
Yes, this code is valid.
Keep in mind that (for C++17), the compiler will semantically replace the range-based for loop by the construct
You see, the lifetime of the
initializer_listis extended to the lifetime of__rangeinside of the outermost scope in the replacement.Note however that you still can easily cause undefined behavior if the range expression contains a temporary itself:
The above code will result in a dangling reference in <= C++20. Only in C++23, the lifetime of the temporary created by
foo()will get extended such that it becomes valid. See also https://en.cppreference.com/w/cpp/language/range-for