]> granicus.if.org Git - clang/commitdiff
In the conflict between C++11 [expr.prim.general]p4, which declares
authorDouglas Gregor <dgregor@apple.com>
Tue, 21 Feb 2012 22:51:27 +0000 (22:51 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 21 Feb 2012 22:51:27 +0000 (22:51 +0000)
that 'this' can be used in the brace-or-equal-initializer of a
non-static data member, and C++11 [expr.prim.lambda]p9, which says
that lambda expressions not in block scope can have no captures, side
fully with C++11 [expr.prim.general]p4 by allowing 'this' to be
captured within these initializers. This seems to be the intent of
non-static data member initializers.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151101 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseExprCXX.cpp
test/CXX/expr/expr.prim/expr.prim.general/p4-0x.cpp

index fecf7b77657d1afb08886e4147b00ec24a0def91..af532de385d84dea358746573ac5c696b3455926 100644 (file)
@@ -870,8 +870,10 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
 
   // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
   // it.
-  ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope |
-                             Scope::DeclScope);
+  unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
+  if (getCurScope()->getFlags() & Scope::ThisScope)
+    ScopeFlags |= Scope::ThisScope;
+  ParseScope BodyScope(this, ScopeFlags);
 
   Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
 
index b976e1664e1fe59ee95c7c194d4c2f1953b37722..b9f0414e917a5be46ad8ac4fa9f0f9e07301f685 100644 (file)
@@ -7,3 +7,14 @@ struct S {
   int arr[sizeof(this)]; // expected-error {{invalid use of 'this' outside of a nonstatic member function}}
   int sz = sizeof(this); // ok
 };
+
+namespace CaptureThis {
+  struct X {
+    int n = 10;
+    int m = [&]{return n + 1; }();
+    int o = [&]{return this->m + 1; }();
+    int p = [&]{return [&](int x) { return this->m + x;}(o); }();
+  };
+  
+  X x;
+}