<div dir="ltr">I was trying to incorporate the lifetime bound in an OSS project (<a href="https://github.com/capnproto/capnproto/pull/1246">https://github.com/capnproto/capnproto/pull/1246</a>) and it surfaced a potential limitation of the annotation (or perhaps I'm just not utilizing its full power yet).<div><br></div><div>Is there a way to use the lifetime annotation to have the compiler track the lifetime through copies? For example, if <font face="monospace">std::string</font>'s conversion operator to <font face="monospace">std::string_view</font> is annotated, will copying/moving/assigning the returned std::string_view (e.g. when building up a <font face="monospace">vector</font> of <font face="monospace">string_view</font> in a loop but accidentally having that <font face="monospace">string_view</font> point to a <font face="monospace">string</font> local to the loop) cause the compiler to still see the copies as having their lifetimes tied to the original std::string? One would think that defaulted copies/moves/assignments would automatically preserve the lifetime & there would need to be some kind of additional annotation for the more complex use-cases, but from my testing this is perhaps not yet implemented?.</div><div><br></div><div>Relatedly, it seems even as something as simple as below didn't trigger the warning so I'm wondering if I'm misusing it or if the lifetime tracking is just very limited in the cases it can find.</div><div><br></div><div><font face="monospace">struct Foo {</font></div><div><font face="monospace">  std::string buf;</font></div><div><font face="monospace">  operator std::string_view() const [[clang::lifetimebound]] {</font></div><div><font face="monospace">    return buf;</font></div><div><font face="monospace">  }</font></div><div><font face="monospace">};</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">int main() {</font></div><div><font face="monospace">  for (int i = 0; i < 5; i++) {</font></div><div><font face="monospace">    std::string_view x;</font></div><div><font face="monospace">    {</font></div><div><font face="monospace">      Foo str = { .buf = std::to_string(i) };</font></div><div><font face="monospace">      x = str;</font></div><div><font face="monospace">    }</font></div><div><font face="monospace">    std::cerr << x << "\n";</font></div><div><font face="monospace">  }</font></div><div><font face="monospace">}</font></div><div><br></div><div><font face="monospace">clang++ -std=c++20 -Wall</font> doesn't seem to generate a warning (v13 from revision 0e92cbd6a652c). Adding <font face="monospace">-fsanitize=address</font> does cause the program to crash when run.</div><div><br></div><div>Thanks,</div><div>Vitali</div></div>