[clang] [Clang] Implement P2718R0 "Lifetime extension in range-based for loops" (PR #76361)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 26 06:40:02 PST 2024


================
@@ -1361,6 +1367,20 @@ class Sema final {
     // VLAs).
     bool InConditionallyConstantEvaluateContext = false;
 
+    /// Whether we are currently in a context in which all temporaries must be
+    /// lifetime-extended, even if they're not bound to a reference (for example,
+    /// in a for-range initializer).
+    bool IsInLifetimeExtendingContext = false;
+
+    /// Whether we should materialize temporaries in discarded expressions.
+    ///
+    /// [C++23][class.temporary]/p2.6 when a prvalue that has type other than cv
+    /// void appears as a discarded-value expression ([expr.context]).
+    ///
+    /// We do not materialize temporaries by default in order to avoid creating
+    /// unnecessary temporary objects.
+    bool MaterializePRValueInDiscardedExpression = false;
----------------
yronglin wrote:

I think this variable and related function `needMaterializePRValueInDiscardedExpression()` should be renamed to  `NeedMaterializePRValue` and `needMaterializePRValue()` , then it can use in other cases.

In [class.temporary P2][http://eel.is/c++draft/class.temporary#2]

The materialization of a temporary object is generally delayed as long as possible in order to avoid creating unnecessary temporary objects[.](http://eel.is/c++draft/class.temporary#2.sentence-1)
[Note [3](http://eel.is/c++draft/class.temporary#note-3): Temporary objects are materialized:
[(2.1)](http://eel.is/c++draft/class.temporary#2.1)
when binding a reference to a prvalue ([[dcl.init.ref]](http://eel.is/c++draft/dcl.init.ref), [[expr.type.conv]](http://eel.is/c++draft/expr.type.conv), [[expr.dynamic.cast]](http://eel.is/c++draft/expr.dynamic.cast), [[expr.static.cast]](http://eel.is/c++draft/expr.static.cast), [[expr.const.cast]](http://eel.is/c++draft/expr.const.cast), [[expr.cast]](http://eel.is/c++draft/expr.cast)),
[(2.2)](http://eel.is/c++draft/class.temporary#2.2)
when performing member access on a class prvalue ([[expr.ref]](http://eel.is/c++draft/expr.ref), [[expr.mptr.oper]](http://eel.is/c++draft/expr.mptr.oper)),
[(2.3)](http://eel.is/c++draft/class.temporary#2.3)
when performing an array-to-pointer conversion or subscripting on an array prvalue ([[conv.array]](http://eel.is/c++draft/conv.array), [[expr.sub]](http://eel.is/c++draft/expr.sub)),
[(2.4)](http://eel.is/c++draft/class.temporary#2.4)
when initializing an object of type std​::​initializer_list<T> from a [braced-init-list](http://eel.is/c++draft/dcl.init.general#nt:braced-init-list) ([[dcl.init.list]](http://eel.is/c++draft/dcl.init.list)),
[(2.5)](http://eel.is/c++draft/class.temporary#2.5)
for certain unevaluated operands ([[expr.typeid]](http://eel.is/c++draft/expr.typeid), [[expr.sizeof]](http://eel.is/c++draft/expr.sizeof)), and
[(2.6)](http://eel.is/c++draft/class.temporary#2.6)
when a prvalue that has type other than cv void appears as a discarded-value expression ([[expr.context]](http://eel.is/c++draft/expr.context))[.](http://eel.is/c++draft/class.temporary#2.sentence-2)
— end note]

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


More information about the cfe-commits mailing list