G++以及clang的处理VLAS定义和初始化的C++&#x24
源代码如下:
using namespace std;
int main()
{
int m = 4;
int arr[m] = {1, 2, 3, 4};
printf("%d\n", arr[2]);
return 0;
}
当我使用 g ++ 编译时,它将成功编译为可执行文件。但是,当我用 clang ++ 编译时,我会收到以下错误:
VLAs.cpp:8:10: error: variable-sized object may not be initialized
int arr[m] = {1, 2, 3, 4};
^
1 error generated.
测试后,我发现clang ++可以支持VLBS的定义。 ,但在初始化它们时不支持定义VLB。我想知道这种差异的根本原因
The source code as follows:
using namespace std;
int main()
{
int m = 4;
int arr[m] = {1, 2, 3, 4};
printf("%d\n", arr[2]);
return 0;
}
When I compile with g++, it compiles successfully as an executable. But when I compile with clang++, I get the following error:
VLAs.cpp:8:10: error: variable-sized object may not be initialized
int arr[m] = {1, 2, 3, 4};
^
1 error generated.
After testing, I found that clang ++ can support the definition of VLBs(int arr[m];
), but does not support defining VLBs while initializing them. I would like to know the root cause of this difference
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VLA不是C ++,编译器以不同的方式支持它们。在C初始化VLA中,不支持VLA,这似乎也是Clang在C ++中所做的。另一方面,海湾合作委员会稍微扩大了对VLA的支持。您的下一个编译器可能根本没有VLA。
那么,如果C ++对此具有较高的机制,为什么要打扰:
VLAs are not C++ and compiler support them differently. In C initializing VLAs is not supported and that seems to be what clang does in c++ as well. gcc on the other hand has extended the support for VLAs a bit. Your next compiler might not have VLAs at all.
So why bother if C++ has far superior mechanisms for this: