The constexpr code rejects uses of 'this' in a constant-expression, but that shouldn't apply to a lambda 'this' capture proxy, which can have a constant value.
Tested x86_64-pc-linux-gnu, applying to trunk and 7.
commit 1f69899dda31109c95ac4636eb66788449d6382e Author: Jason Merrill <ja...@redhat.com> Date: Wed May 31 14:45:04 2017 -0400 * constexpr.c (potential_constant_expression_1): Allow 'this' capture. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 157b20d..8bbe950 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5296,7 +5296,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, { tree x = TREE_OPERAND (t, 0); STRIP_NOPS (x); - if (is_this_parameter (x)) + if (is_this_parameter (x) && !is_capture_proxy (x)) { if (DECL_CONTEXT (x) && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x))) diff --git a/gcc/testsuite/g++.dg/cpp1z/lambda-this3.C b/gcc/testsuite/g++.dg/cpp1z/lambda-this3.C new file mode 100644 index 0000000..b5a1fc6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/lambda-this3.C @@ -0,0 +1,10 @@ +// { dg-options -std=c++1z } + +struct S { + int i; + constexpr S() : i(5) { + ([*this] () { return i + 10; }()); + } + constexpr operator int() const { return i; } +}; +constexpr int x = S();