From 130ee9a9fc802926d2fe2701e041c98a6fb0f981 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 22 Jan 2016 15:36:30 -0500 Subject: [PATCH] re PR c++/69392 (G++ can't capture 'this' pointer to templated type using init-capture) PR c++/69392 * lambda.c (lambda_capture_field_type): Handle 'this' specially for init-capture, too. From-SVN: r232746 --- gcc/cp/ChangeLog | 4 ++++ gcc/cp/lambda.c | 18 +++++++++--------- gcc/testsuite/g++.dg/cpp1y/lambda-init14.C | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-init14.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index af5c907713e7..05ca52a2cbc7 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2016-01-21 Jason Merrill + PR c++/69392 + * lambda.c (lambda_capture_field_type): Handle 'this' specially + for init-capture, too. + PR c++/65687 * decl.c (type_is_deprecated): Don't look into a typedef. diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c index 3b0ea182b2a5..93b192c46f17 100644 --- a/gcc/cp/lambda.c +++ b/gcc/cp/lambda.c @@ -207,15 +207,8 @@ tree lambda_capture_field_type (tree expr, bool explicit_init_p) { tree type; - if (explicit_init_p) - { - type = make_auto (); - type = do_auto_deduction (type, expr, type); - } - else - type = non_reference (unlowered_expr_type (expr)); - if (type_dependent_expression_p (expr) - && !is_this_parameter (tree_strip_nop_conversions (expr))) + bool is_this = is_this_parameter (tree_strip_nop_conversions (expr)); + if (!is_this && type_dependent_expression_p (expr)) { type = cxx_make_type (DECLTYPE_TYPE); DECLTYPE_TYPE_EXPR (type) = expr; @@ -223,6 +216,13 @@ lambda_capture_field_type (tree expr, bool explicit_init_p) DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p; SET_TYPE_STRUCTURAL_EQUALITY (type); } + else if (!is_this && explicit_init_p) + { + type = make_auto (); + type = do_auto_deduction (type, expr, type); + } + else + type = non_reference (unlowered_expr_type (expr)); return type; } diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-init14.C b/gcc/testsuite/g++.dg/cpp1y/lambda-init14.C new file mode 100644 index 000000000000..f7fffc5127dd --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-init14.C @@ -0,0 +1,19 @@ +// PR c++/69392 +// { dg-do compile { target c++14 } } + +template +class Foo { + public: + void foo(void) {} + auto getCallableFoo(void) { + return + [ptr = this]() { ptr->foo(); }; + } +}; + +int main() +{ + Foo f; + auto callable = f.getCallableFoo(); + callable(); +} -- 2.47.2