2017-01-20 30 views
18

我有以下SSCCE:GCC導致段錯誤的λ-拍攝參數包

#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); 
} 

在鐺++(3.9.1)這個編譯併發出的 「Hello World」。 Gcc 6.3在-O3下發生分段錯誤。

我可以通過明確地傳遞指針和包裝來解決問題,用[&args...]()代替[&]()。但是,到現在爲止,我認爲[&]將會像逐個列出所有參數一樣。

那麼,什麼是走錯了嗎?

P.S: 這不限於-O3-O0不段錯誤,但不會返回預期的結果(的 「Hello World!」):

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

[:~/tmp] $ 

P.P.S:進一步降低SSCCE。現在我甚至不會再用-Wall -Wextra進行診斷。

+0

似乎只與優化發生。 – chris

+0

無法確認 - 同樣發生在'-O0'上(參見附錄) – mrks

+1

@Exagon,當然可以通過引用來傳遞。但它不能被複制。這裏有一個例子:http://melpon.org/wandbox/permlink/lj4SQhth5iis81FG – chris

回答

9

我強烈懷疑G ++錯誤


這裏有一些注意事項:

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.

+1

謝謝你關注此事。我在https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79180提交了一個錯誤 – mrks