2013-04-22 Jason Merrill <jason@redhat.com>
+ N3648
+ * parser.c (cp_parser_lambda_introducer): Make lambda capture init
+ pedwarn unconditional except in C++1y mode.
+
* semantics.c (potential_constant_expression_1): Don't crash on
'this' in NSDMI.
{
/* An explicit expression exists. */
cp_lexer_consume_token (parser->lexer);
- pedwarn (input_location, OPT_Wpedantic,
- "ISO C++ does not allow initializers "
- "in lambda expression capture lists");
+ if (cxx_dialect < cxx1y)
+ pedwarn (input_location, 0,
+ "lambda capture initializers "
+ "only available with -std=c++1y or -std=gnu++1y");
capture_init_expr = cp_parser_assignment_expression (parser,
/*cast_p=*/true,
&idk);
+++ /dev/null
-// Test for the explicit initializer extension
-// { dg-options "-std=c++0x" }
-
-int main()
-{
- int j = [i = 2]{sizeof(i); return i;}();
- return (j != 2);
-}
// Testcase for an extension to allow return type deduction when the lambda
// contains more than just a single return-statement.
-// { dg-options -std=c++0x }
+// { dg-options -std=c++1y }
// { dg-do run }
bool b;
--- /dev/null
+// Test for the explicit initializer extension of C++1y
+// { dg-options "-std=c++1y" }
+
+int main()
+{
+ int j = [i = 2]{sizeof(i); return i;}();
+ return (j != 2);
+}
--- /dev/null
+// N3648: capture init
+// { dg-options "-std=c++1y -w" }
+// { dg-do run }
+
+int main()
+{
+ int x = 41;
+ auto r = [x = x+1]{ return x; }();
+ if (r != 42) __builtin_abort();
+
+ static auto *p = &r;
+ [&x=r]{ if (&x != p) __builtin_abort(); }();
+}
--- /dev/null
+// N3648: redundancy and capture init
+// { dg-options "-std=c++1y -pedantic-errors" }
+
+int main()
+{
+ int x = 42;
+ [=,x]{}; // { dg-error "redundant" }
+ [=,&x]{};
+ [&,&x]{}; // { dg-error "redundant" }
+ [&,x]{};
+ [=,x=24]{};
+ [&,&r=x]{};
+}
--- /dev/null
+// N3648: capture init at non-block scope
+// { dg-options "-std=c++1y -w" }
+// { dg-do run }
+
+int i = 42;
+int j = [x=i]{ return x; }();
+
+int main()
+{
+ if (j != 42) __builtin_abort();
+}
--- /dev/null
+// N3648: capture init example from paper
+// { dg-options "-std=c++1y" }
+// { dg-do run }
+
+int x = 4;
+auto y = [&r = x, x = x+1]()->int {
+ r += 2;
+ return x+2;
+}(); // Updates ::x to 6, and initializes y to 7.
+
+int main()
+{
+ if (x != 6 || y != 7) __builtin_abort();
+}