From: Douglas Gregor Date: Fri, 17 Feb 2012 04:02:59 +0000 (+0000) Subject: Only add 'const' to the type of variables captured in a lambda when X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd64520ca4e4d4c531637d311f8ea384c912fce8;p=clang Only add 'const' to the type of variables captured in a lambda when we're capturing it by value in a non-mutable lambda. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150791 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 5202f96354..c15102c4da 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9575,8 +9575,13 @@ static bool shouldAddConstForScope(CapturingScopeInfo *CSI, VarDecl *VD) { return false; if (isa(CSI)) return true; - if (LambdaScopeInfo *LSI = dyn_cast(CSI)) - return !LSI->Mutable; + if (LambdaScopeInfo *LSI = dyn_cast(CSI)) { + if (LSI->isCaptured(VD)) + return LSI->getCapture(VD).isCopyCapture() && !LSI->Mutable; + + return LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByval && + !LSI->Mutable; + } return false; } diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p18.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p18.cpp index 561ead1271..03cbe32ef4 100644 --- a/test/CXX/expr/expr.prim/expr.prim.lambda/p18.cpp +++ b/test/CXX/expr/expr.prim/expr.prim.lambda/p18.cpp @@ -38,4 +38,8 @@ void f3() { "should be const float&"); }(); }(); + + [&i] { + static_assert(is_same::value, "should be int&"); + }(); }