简体   繁体   中英

GCC causes segfault for lambda-captured parameter pack

I have the following SSCCE:

#include <iostream>
#include <string>

void foo(const std::string &a) {
  std::cout << a << std::endl;
}

template <typename... Args>
void bar(Args &&... args) {
  [&]() {
    [&]() {
          foo(args...);
      }();
  }();
}

int main() {
 const std::string x("Hello World!");
 bar(x);
}

Under clang++ (3.9.1) this compiles and emits "Hello World". Gcc 6.3 fails with a segmentation fault under -O3 .

I can fix the problem by explicitly passing the pointer and the pack by reference, replacing [&]() with [&args...]() . However, up to now, I thought that [&] would do the same as listing all arguments one by one.

So what is going wrong here?

PS: This is not limited to -O3 . -O0 does not segfault but does not return the expected result ("Hello World!"):

[:~/tmp] $ g++-6 -std=c++1z param.cpp && ./a.out

[:~/tmp] $

PPS: Further reduced SSCCE. Now I don't even get a diagnostic with -Wall -Wextra anymore.

I strongly suspect a g++ bug .


Here are some notes:

internal compiler error: in make_decl_rtl, at varasm.c:1304

...

Please submit a full bug report, with preprocessed source if appropriate.

Please include the complete backtrace with any bug report. See http://gcc.gnu.org/bugs.html for instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM