Closed mgehre closed 5 years ago
On
#include <string> #include <vector> void f(std::vector<std::string> v) { for(std::string &r: v) { r += ""; r.back(); } }
we warn
<source>:7:9: warning: dereferencing a dangling pointer [-Wlifetime] r.back(); <source>:6:9: note: modified here r += "";
but r += "" modifies the string, not the reference, so the reference does not dangle.
r += ""
https://godbolt.org/z/wbvyfd
fyi @Xazax-hun
A slightly smaller repro (eliminated the loop):
#include <string> #include <vector> void f(std::vector<std::string> v) { std::string &r = v[0]; r += "aa"; r.back(); }
On
we warn
but
r += ""
modifies the string, not the reference, so the reference does not dangle.https://godbolt.org/z/wbvyfd
fyi @Xazax-hun