@@ -13950,6 +13950,34 @@ static tree
extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
tree *cond_guard)
{
+ /* CWG1299 (C++20): The temporary object to which the reference is bound or
+ the temporary object that is the complete object of a subobject to which
+ the reference is bound persists for the lifetime of the reference if the
+ glvalue to which the reference is bound was obtained through one of the
+ following:
+ - a temporary materialization conversion ([conv.rval]),
+ - ( expression ), where expression is one of these expressions,
+ - subscripting ([expr.sub]) of an array operand, where that operand is one
+ of these expressions,
+ - a class member access ([expr.ref]) using the . operator where the left
+ operand is one of these expressions and the right operand designates a
+ non-static data member of non-reference type,
+ - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
+ where the left operand is one of these expressions and the right operand
+ is a pointer to data member of non-reference type,
+ - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
+ dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
+ ([expr.reinterpret.cast]) converting, without a user-defined conversion,
+ a glvalue operand that is one of these expressions to a glvalue that
+ refers to the object designated by the operand, or to its complete
+ object or a subobject thereof,
+ - a conditional expression ([expr.cond]) that is a glvalue where the
+ second or third operand is one of these expressions, or
+ - a comma expression ([expr.comma]) that is a glvalue where the right
+ operand is one of these expressions. */
+
+ /* FIXME several cases are still handled wrong (101572, 81420). */
+
tree sub = init;
tree *p;
STRIP_NOPS (sub);
@@ -13960,6 +13988,16 @@ extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
cond_guard);
return init;
}
+ if (TREE_CODE (sub) == POINTER_PLUS_EXPR
+ && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
+ (TREE_OPERAND (sub, 1)))))
+ {
+ /* A pointer-to-member operation. */
+ TREE_OPERAND (sub, 0)
+ = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
+ cond_guard);
+ return init;
+ }
if (TREE_CODE (sub) == COND_EXPR)
{
tree cur_cond_guard = NULL_TREE;
new file mode 100644
@@ -0,0 +1,39 @@
+// PR c++/53288
+// { dg-do compile { target c++11 } }
+
+struct B {
+ B(int data) : _data(data) { }
+ ~B() { }
+
+ int _data;
+
+private:
+ B() = delete;
+ B(const B &) = delete;
+ B(B &&) = delete;
+};
+
+int c,d;
+struct A {
+ B b;
+ A(int data) : b(data) { ++c; }
+ ~A() { ++d; }
+
+private:
+ A() = delete;
+ A(const A &) = delete;
+ A(A &&) = delete;
+};
+
+template <class T>
+void f(T t) {
+ const B &b = A(1).*t;
+ if (d) __builtin_abort ();
+}
+
+int main() {
+ const B &b = A(1).*(&A::b);
+ if (d) __builtin_abort ();
+
+ f(&A::b);
+}