Index: clang/lib/AST/Interp/ByteCodeEmitter.h =================================================================== --- clang/lib/AST/Interp/ByteCodeEmitter.h +++ clang/lib/AST/Interp/ByteCodeEmitter.h @@ -73,7 +73,7 @@ /// Lambda captures. /// Map from Decl* to [Offset, IsReference] pair. llvm::DenseMap> LambdaCaptures; - unsigned LambdaThisCapture; + unsigned LambdaThisCapture = 0; /// Local descriptors. llvm::SmallVector, 2> Descriptors; Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeEmitter.cpp +++ clang/lib/AST/Interp/ByteCodeEmitter.cpp @@ -63,8 +63,8 @@ this->LambdaCaptures[Cap.first] = { Offset, Cap.second->getType()->isReferenceType()}; } - // FIXME: LambdaThisCapture - (void)LTC; + if (LTC) + this->LambdaThisCapture = R->getField(LTC)->Offset; } } Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -1838,6 +1838,10 @@ bool ByteCodeExprGen::VisitCXXThisExpr(const CXXThisExpr *E) { if (DiscardResult) return true; + + if (this->LambdaThisCapture > 0) + return this->emitGetThisFieldPtr(this->LambdaThisCapture, E); + return this->emitThis(E); } Index: clang/lib/AST/Interp/EvalEmitter.h =================================================================== --- clang/lib/AST/Interp/EvalEmitter.h +++ clang/lib/AST/Interp/EvalEmitter.h @@ -79,7 +79,7 @@ /// Lambda captures. /// Map from Decl* to [Offset, IsReference] pair. llvm::DenseMap> LambdaCaptures; - unsigned LambdaThisCapture; + unsigned LambdaThisCapture = 0; /// Local descriptors. llvm::SmallVector, 2> Descriptors; Index: clang/test/AST/Interp/lambda.cpp =================================================================== --- clang/test/AST/Interp/lambda.cpp +++ clang/test/AST/Interp/lambda.cpp @@ -107,3 +107,23 @@ static_assert(foo() == 1); // expected-error {{not an integral constant expression}} } +namespace ThisCapture { + class Foo { + public: + int b = 32; + int a; + + constexpr Foo() : a([this](){ return b + 1;}()) {} + + constexpr int Aplus2() const { + auto F = [this]() { + return a + 2; + }; + + return F(); + } + }; + constexpr Foo F; + static_assert(F.a == 33, ""); + static_assert(F.Aplus2() == (33 + 2), ""); +}