为什么这个看起来无效的代码在 g++ 6.0 上编译成功?
Why does this invalid-looking code compile successfully on g++ 6.0?
考虑这个奇怪的程序:
int main()
{
int(*){} Is it C++14 or any other language?
}
即使缺少注释 //
, 即使我在 g++ 6.0 中使用 -pedantic-errors
选项,代码也可以正常编译,没有任何错误和警告。这对我来说似乎是一个编译器错误。真的是编译器的bug吗?
在我可以测试它的所有版本中,这看起来是一个带有 g++ 的 bug/feature/issue。 运行
int main()
{
int(*){} Is it C++14 or any other language?
}
在 godbolt.org 上,对于所有没有编译标志的 g++ 版本,给出以下程序集输出。
main:
pushq %rbp
movq %rsp, %rbp
movl [=11=], %eax
leave
ret
我得到的唯一诊断是 godbolt.org,也就是
!!warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
Clang、ICC 和 MSVS 都编译失败。
编辑:
来自评论zwol filed a bug with gcc on this. The bug report can be found here.
我在 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?
^
然而,它仍然能够编译...所以我 dumped the 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
它太大而无法放在评论中,但应该有助于确定正在发生的事情。我仍在经历这个,但我只是发布此信息供其他人构建。
可视化如下。
考虑这个奇怪的程序:
int main()
{
int(*){} Is it C++14 or any other language?
}
即使缺少注释 //
, 即使我在 g++ 6.0 中使用 -pedantic-errors
选项,代码也可以正常编译,没有任何错误和警告。这对我来说似乎是一个编译器错误。真的是编译器的bug吗?
在我可以测试它的所有版本中,这看起来是一个带有 g++ 的 bug/feature/issue。 运行
int main()
{
int(*){} Is it C++14 or any other language?
}
在 godbolt.org 上,对于所有没有编译标志的 g++ 版本,给出以下程序集输出。
main:
pushq %rbp
movq %rsp, %rbp
movl [=11=], %eax
leave
ret
我得到的唯一诊断是 godbolt.org,也就是
!!warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
Clang、ICC 和 MSVS 都编译失败。
编辑:
来自评论zwol filed a bug with gcc on this. The bug report can be found here.
我在 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?
^
然而,它仍然能够编译...所以我 dumped the 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
它太大而无法放在评论中,但应该有助于确定正在发生的事情。我仍在经历这个,但我只是发布此信息供其他人构建。
可视化如下