2016-02-26 44 views
6

當使用通用lambdas(允許使用auto作爲lambda參數的C++ 14功能)時,最近我遇到了GCC的一個有趣的怪癖。看起來,使用auto而不是type,會以某種方式影響「this」指針被捕獲的方式。我在下面的例子中再現了問題:在C++中使用泛型lambda時出現奇怪的編譯器行爲14

#include <cstdio> 

class A { private: 
    void method(int i) { 
     printf("Method Call %d",i); 
    } 

public: 
    void publicMethod() { 
     auto lambda = [&] (const int k) { //problematic line 
      method(k);     //problematic line 
     }; 
     lambda(42); 
    } }; 

int main() { 
    A a; 
    a.publicMethod(); 
    return 0; } 

代碼上面的工作。當我改變 「有問題行」 到:

 auto lambda = [&] (const auto k) { //problematic line 
      method(k);      //problematic line 

這不是編了,但是加入 「this」 指針:

 auto lambda = [&] (const auto k) { //problematic line 
      this->method(k);    //problematic line 

解決問題。我的問題是:這種行爲有沒有理由?還是GCC的非理性怪癖(也許應該報告)?

以下是編譯器輸出的情況下,與auto使用無this

test.cpp: In lambda function: 
test.cpp:12:13: internal compiler error: Segmentation fault 
      method(k); 
      ^
0x848ecc crash_signal 
    ../../src/gcc/toplev.c:383 
0xea28e5 size_binop_loc(unsigned int, tree_code, tree_node*, tree_node*) 
    ../../src/gcc/fold-const.c:1768 
0xee86d7 gimplify_compound_lval 
    ../../src/gcc/gimplify.c:2039 
0xee86d7 gimplify_expr(tree_node**, gimple_statement_base**, gimple_statement_base**, bool (*)(tree_node*), int) 
    ../../src/gcc/gimplify.c:8052 
0xeeb639 gimplify_arg(tree_node**, gimple_statement_base**, unsigned int) 
    ../../src/gcc/gimplify.c:2271 
0xeeb639 gimplify_call_expr 
    ../../src/gcc/gimplify.c:2477 
0xee89f2 gimplify_expr(tree_node**, gimple_statement_base**, gimple_statement_base**, bool (*)(tree_node*), int) 
    ../../src/gcc/gimplify.c:8071 
0xee8da7 gimplify_stmt(tree_node**, gimple_statement_base**) 
    ../../src/gcc/gimplify.c:5519 
0xee8da7 gimplify_cleanup_point_expr 
    ../../src/gcc/gimplify.c:5295 
0xee8da7 gimplify_expr(tree_node**, gimple_statement_base**, gimple_statement_base**, bool (*)(tree_node*), int) 
    ../../src/gcc/gimplify.c:8463 
0xee7ace gimplify_stmt(tree_node**, gimple_statement_base**) 
    ../../src/gcc/gimplify.c:5519 
0xee94db gimplify_bind_expr 
    ../../src/gcc/gimplify.c:1136 
0xee94db gimplify_expr(tree_node**, gimple_statement_base**, gimple_statement_base**, bool (*)(tree_node*), int) 
    ../../src/gcc/gimplify.c:8297 
0xee757f gimplify_stmt(tree_node**, gimple_statement_base**) 
    ../../src/gcc/gimplify.c:5519 
0xee757f gimplify_body(tree_node*, bool) 
    ../../src/gcc/gimplify.c:9234 
0xee72fd gimplify_function_tree(tree_node*) 
    ../../src/gcc/gimplify.c:9388 
0xe195e0 cgraph_node::analyze() 
    ../../src/gcc/cgraphunit.c:634 
0xe195e0 analyze_functions 
    ../../src/gcc/cgraphunit.c:1024 
0x127d1ab symbol_table::finalize_compilation_unit() 
    ../../src/gcc/cgraphunit.c:2453 
0xd37db5 cp_write_global_declarations() 
    ../../src/gcc/cp/decl2.c:4843 
Please submit a full bug report, 
with preprocessed source if appropriate. 
Please include the complete backtrace with any bug report. 
See <file:///usr/share/doc/gcc-5/README.Bugs> for instructions. 

與GCC 5.3.1

+12

'內部編譯器error'這是一個編譯器錯誤。 – bolov

+0

在叮噹它[作爲縮進](http://melpon.org/wandbox/permlink/lTOF7QxfICP6sW0l)。 –

回答