为什么这段看起来无效的代码能够在 g + + 6.0上成功编译?

想想这个奇怪的程序:

int main()
{
int(*){} Is it C++14 or any other language?
}

(请看现场演示 给你给你。)

即使没有注释 //,即使在 g + + 6.0中使用 -pedantic-errors选项,代码也能很好地编译,没有任何错误和警告。在我看来,这像是一个编译器错误。它真的是编译器中的一个 bug 吗?

3562 次浏览

这看起来是 g + + 的一个 bug/功能/问题,在所有我可以测试它的版本中都是如此

int main()
{
int(*){} Is it C++14 or any other language?
}

对于所有没有编译标志的 g + + 版本,在 Godbolt.org上给出以下程序集输出。

main:
pushq   %rbp
movq    %rsp, %rbp
movl    $0, %eax
leave
ret

我得到的唯一诊断是 Godbolt.org

!!warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

Clang、 ICC 和 MSVS 都无法编译此文件。

编辑:

从评论 Zwol提交了一个错误与 gcc 对此。错误报告可以找到 给你

我使用 g++版本的 5.1.1在我的 Fedora VM 上运行了这个命令,发现了以下内容:

[user:~] 1 $ g++ -fdump-tree-original-raw tmp.cpp
tmp.cpp: In function ‘int main()’:
tmp.cpp:3:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int(*){} Is it C++14 or any other language?
^

然而,这仍然设法编译... 所以我有 排除了 AST和得到这个:

$ cat tmp.cpp.003t.original


;; Function int main() (null)
;; enabled by -tree-original


@1      return_expr      type: @2       expr: @3
@2      void_type        name: @4       algn: 8
@3      init_expr        type: @5       op 0: @6       op 1: @7
@4      type_decl        name: @8       type: @2       srcp: <built-in>:0
note: artificial
@5      integer_type     name: @9       size: @10      algn: 32
prec: 32       sign: signed   min : @11
max : @12
@6      result_decl      type: @5       scpe: @13      srcp: tmp.cpp:1
note: artificial              size: @10
algn: 32
@7      integer_cst      type: @5      int: 0
@8      identifier_node  strg: void     lngt: 4
@9      type_decl        name: @14      type: @5       srcp: <built-in>:0
note: artificial
@10     integer_cst      type: @15     int: 32
@11     integer_cst      type: @5      int: -2147483648
@12     integer_cst      type: @5      int: 2147483647
@13     function_decl    name: @16      type: @17      scpe: @18
srcp: tmp.cpp:1               lang: C
link: extern
@14     identifier_node  strg: int      lngt: 3
@15     integer_type     name: @19      size: @20      algn: 128
prec: 128      sign: unsigned min : @21
max : @22
@16     identifier_node  strg: main     lngt: 4
@17     function_type    size: @23      algn: 8        retn: @5
prms: @24
@18     translation_unit_decl
@19     identifier_node  strg: bitsizetype             lngt: 11
@20     integer_cst      type: @15     int: 128
@21     integer_cst      type: @15     int: 0
@22     integer_cst      type: @15     int: -1
@23     integer_cst      type: @15     int: 8
@24     tree_list        valu: @2

它太大以至于无法放入评论中,但是对于确定正在发生的事情应该是有用的。我仍然在通过这一点,但我只是张贴这一信息,其他人从建设。

就像这个 < img src = “ https://i.stack.imgur.com/GOfrP.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/GOfrP.png”alt = “ enter image description here”>