diff mbox series

[PR,c++/81574] lambda capture of function reference

Message ID 3813cfdf-4b72-728b-f6f7-a56f1c20126c@acm.org
State New
Headers show
Series [PR,c++/81574] lambda capture of function reference | expand

Commit Message

Nathan Sidwell Nov. 15, 2017, 1:15 p.m. UTC
This patch fixes 81574.  Even when the capture default is '=', a 
reference to a function is captured by reference.  The init-capture case 
captures a pointer, via auto deduction machinery.  AFAICT that's the 
correct behaviour.

applying to trunk.

nathan
diff mbox series

Patch

2017-11-15  Nathan Sidwell  <nathan@acm.org>

	PR c++/81574
	* lambda.c (lambda_capture_field_type): Function references are
	always catured by reference.

	PR c++/81574
	* g++.dg/cpp1y/pr81574.C: New.

Index: cp/lambda.c
===================================================================
--- cp/lambda.c	(revision 254740)
+++ cp/lambda.c	(working copy)
@@ -245,7 +245,8 @@  lambda_capture_field_type (tree expr, bo
     {
       type = non_reference (unlowered_expr_type (expr));
 
-      if (!is_this && by_reference_p)
+      if (!is_this
+	  && (by_reference_p || TREE_CODE (type) == FUNCTION_TYPE))
 	type = build_reference_type (type);
     }
 
Index: testsuite/g++.dg/cpp1y/pr81574.C
===================================================================
--- testsuite/g++.dg/cpp1y/pr81574.C	(revision 0)
+++ testsuite/g++.dg/cpp1y/pr81574.C	(working copy)
@@ -0,0 +1,13 @@ 
+// { dg-do compile { target c++14 } }
+// PR c++/81574 references to functions are captured by reference.
+
+// 8.1.5.2/10
+// For each entity captured by copy, ... an lvalue reference to the
+// referenced function type if the entity is a reference to a function
+
+void f (void (&b)())
+{
+  [=] {  b; } ();
+  [=, b(f)] { b; } ();
+  [=, b(b)] { b; } ();
+}