难道" INT大小= 10;"产生一个恒定的前pression? [英] Does "int size = 10;" yield a constant expression?

查看:118
本文介绍了难道" INT大小= 10;"产生一个恒定的前pression?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code下的gcc编译4.8锵和3.2:

The following code compiles under gcc 4.8 and Clang 3.2:

int main()
{
  int size = 10;
  int arr[size];
}

C ++标准的8.3.4 / 1说,数组的大小必须是整型常量前pression,其中尺寸似乎并没有被。这是两种编译器的一个bug,还是我失去了一些东西?

8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a bug in both compilers, or am I missing something?

最新的VC ++ CTP拒绝code。与这个有趣的消息:

The latest VC++ CTP rejects the code with this interesting message:

error C2466: cannot allocate an array of constant size 0

有趣的部分是它如何似乎认为尺寸是零。但至少它拒绝code。不应该gcc和铛做?

The interesting part is how it seems to think that size is zero. But at least it rejects the code. Shouldn't gcc and Clang do the same?

推荐答案

这是变长数组 VLA 的这是一个的 C99 的功能,但 GCC 支持它作为一个扩展中的 C ++ 的同时,Visual Studio中的does不的。因此,的Visual Studio 是坚持标准在这种情况下,并在技术上是正确的。不是说扩展是坏的, Linux内核取决于许多GCC的扩展的,所以他们可在某些情况下是有用的。

This is variable length arrays or VLA which is a C99 feature but gcc and clang support it as an extension in C++ while Visual Studio does not. So Visual Studio is adhering to the standard in this case and is technically correct. Not to say that extensions are bad, the Linux kernel depends on many gcc extensions, so they can be useful in certain contexts.

如果您添加 -pedantic 标志既 GCC 将就此发出警告,例如 GCC 说(的看直播 的):

If you add the -pedantic flag both gcc and clang will warn you about this, for example gcc says (see it live):

warning: ISO C++ forbids variable length array 'arr' [-Wvla]
  int arr[size];
              ^

使用 -pedantic-错误标志将使这个错误。你可以阅读更多有关这些文件语言标准的扩展,由GCC 和的clangs语言兼容性部分

Using the -pedantic-errors flag will make this an error. You can read more about extensions in these documents Language Standards Supported by GCC and clangs Language Compatibility section.

更新

借助草案C ++标准涵盖的是一个整型常量前pression 的第 5.19 恒前pressions 的段落的 3 说:

The draft C++ standard covers what is a integral constant expression in section 5.19 Constant expressions paragraph 3 and says:

这是整型常量前pression是整数或无作用域枚举类型的前pression,隐式转换为prvalue,其中​​转换前pression是一个核心不变的前pression。 [...]

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [...]

这不是从阅读直观明显这是什么所有的可能性,但 Boost的编码为整型常量前pressions指南做的是伟大的工作。

It is not intuitively obvious from reading this what all the possibilities are but Boost's Coding Guidelines for Integral Constant Expressions does a great job of that .

在此情况下,由于要初始化尺寸文字的使用的常量的就足以使它成为整型常量前pression 的同时也带来了code重新成为标准的 C ++ 的:

In this case since you are initializing size with a literal using const would suffice to make it an integral constant expression and also bring the code back to being standard C++:

const int size = 10;

使用

constexpr 的将工作太:

constexpr int size = 10;

这可能会帮助读 constexpr 差异$ C>和常量

It would probably help to read Difference between constexpr and const.

有关参考相当于部分 8.3.4 段的 1 的中的 C99标准草案将部分 6.7.5.2 数组声明的段落的 4 的它说(的重点煤矿的):

For reference the equivalent section to 8.3.4 paragraph 1 in the C99 draft standard would be section 6.7.5.2 Array declarators paragraph 4 which says (emphasis mine):

如果大小不是present,数组类型是一个不完整的类型。如果大小的*,而不是作为一个前pression,数组类型是一个可变长度的数组类型未指定大小,只能在函数原型范围的声明中使用; 124)这样的阵列仍然完全类型。如果大小是整型常量前pression和元素类型有一个已知常数的大小,数组类型不是变长数组类型; ,否则,数组类型是一个可变长度的数组类型

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

这篇关于难道" INT大小= 10;"产生一个恒定的前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆