[PATCH] D119136: [clang] Implement Change scope of lambda trailing-return-type

Richard Trieu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 18 10:03:02 PDT 2022


rtrieu added a comment.

This seems to be acting weird in template instantations.  Here's an example where the lambda only errors inside a template.

  template <class>
  int foo(int x = 0) {
      auto lambda = [x = x+1]() -> decltype(x) {
          return x;
      };
      return -1;
  }
  
  // no template
  int bar(int x = 0) {
      auto lambda = [x = x+1]() -> decltype(x) {
          return x;
      };
      return -1;
  }
  
  int a = foo<int>();  // error
  int b = bar();  // no  error



  /tmp/lambda.cc:4:16: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified
          return x;
                 ^
  /tmp/lambda.cc:17:9: note: in instantiation of function template specialization 'foo<int>' requested here
  int a = foo<int>();  // error
          ^
  /tmp/lambda.cc:3:20: note: 'x' declared here
      auto lambda = [x = x+1]() -> decltype(x) {
                     ^
  /tmp/lambda.cc:3:19: note: lambda expression begins here
      auto lambda = [x = x+1]() -> decltype(x) {
                    ^
  /tmp/lambda.cc:3:27: note: capture 'x' by value
      auto lambda = [x = x+1]() -> decltype(x) {
                            ^
                            , x
  /tmp/lambda.cc:3:27: note: capture 'x' by reference
      auto lambda = [x = x+1]() -> decltype(x) {
                            ^
                            , &x
  /tmp/lambda.cc:3:20: note: default capture by value
      auto lambda = [x = x+1]() -> decltype(x) {
                     ^
                     =, 
  /tmp/lambda.cc:3:20: note: default capture by reference
      auto lambda = [x = x+1]() -> decltype(x) {
                     ^
                     &, 
  1 error generated.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119136/new/

https://reviews.llvm.org/D119136



More information about the cfe-commits mailing list