[clang] [Clang] Implement C++26 P2748R5 "Disallow Binding a Returned Glvalue to a Temporary" (PR #89942)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 26 03:03:35 PDT 2024


================
@@ -8340,8 +8340,17 @@ void Sema::checkInitializerLifetime(const InitializedEntity &Entity,
             << Entity.getType()->isReferenceType() << CLE->getInitializer() << 2
             << DiagRange;
       } else {
-        Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref)
-         << Entity.getType()->isReferenceType() << DiagRange;
+        // P2748R5: Disallow Binding a Returned Glvalue to a Temporary.
+        // [stmt.return]/p6: In a function whose return type is a reference,
+        // other than an invented function for std::is_convertible ([meta.rel]),
+        // a return statement that binds the returned reference to a temporary
+        // expression ([class.temporary]) is ill-formed.
----------------
yronglin wrote:

> I don't know if Clang implements all the cases in [class.temporary]

AFAIK, I think Clang covered all the cases in [class.temporary](except https://github.com/llvm/llvm-project/pull/87933 and https://github.com/llvm/llvm-project/pull/86960 they still WIP).

> And IMO std::is_nothrow_convertible(_v) should be implementable without intrinsic (and the implementation strategy would be [quite simple](https://en.cppreference.com/w/cpp/types/is_convertible#Possible_implementation)). The __is_nothrow_convertible instrinsic should only be meaningful for acceleration of compilation.

Wow! it's sensible to me now, somehow, I've a local patch to build a hypothetical function declared `void conv-dest(To) noexcept;`.............., I don't know why I would do such a thing, lol. 

https://github.com/llvm/llvm-project/pull/89942


More information about the cfe-commits mailing list