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?
P.S:
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] $
P.P.S: Further reduced SSCCE. Now I don't even get a diagnostic with -Wall -Wextra
anymore.
A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.
The segmentation error is one of the runtime error, that is caused because of the memory access violation, like accessing invalid array index, pointing some restricted address etc.
I strongly suspect a g++ bug.
Here are some notes:
std::string
with any elementary type, e.g., int
still does not work
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With