C ++数组大小声明和const [英] C++ array size declaration and const

查看:139
本文介绍了C ++数组大小声明和const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是跳进C ++,从C来

I'm just jumping into C++, coming from C

在C(89/90),一个常量实际上不是一个常数(而不是一个的#define 'D,枚举,或文字),而是只读设置一次。即,我可以:

In C (89/90), a const is not actually a constant (as opposed to a #define'd, enum, or literal), but rather read-only once set. I.e, I can:

const int x = rand();

这很好 - 点是 X 不知道,直到运行时。因此,我不能

and that's fine - the point being x isn't known until runtime. Hence, I can't

int arr[x]; // error - x is not a compile-time constant

然后,对C类标准(99?)一个人去前进,允许变长数组。虽然我通常code对C中的ANSI标准,这实际上已经产生了影响,现在我想以皮卡C ++ 11。

Then, one of the C standards (99?) went ahead and allowed for variable-length arrays. Although I normally code against the ANSI standard in C, this has actually had an impact now that I am trying to pickup C++11.

据我所知,C ++不允许变长数组。然而,许多编译器允许它作为一个扩展(GCC?)。问题是,现在我想学习C ++ 11,如果有什么我编码为有效的C我不能告诉++或C ++扩展了C99兼容。例如:

As far as I know, C++ does not allow for variable-length arrays. However, many compilers allow it as an extension (GCC ?). The problem is, now that I am trying to learn C++11, I can't tell if what I'm coding is valid C++, or C++ extended with C99-compatibility. Ex:

std::default_random_engine e{};
std::uniform_int_distribution<int> d{};
const int x{d(e)};
int arr[x]; // compiles

我不知道这是否是有效的C ++或没有。显然, x的值不知道,直到运行时。我想我可能不理解C和C之间的差异++ 常量

I can't tell if this is valid C++ or not. Clearly, the value of x is not known until runtime. I think I may not understand the difference between C and C++ const?

推荐答案

您是否正确沃拉斯是C99的功能(的在C11 的),而C可选++标准不包含这个功能虽然两者 GCC 让他们在C ++的扩展。我们可以看到,他们不是要去允许C ++草案标准的11 部分 8.3.4 阵列的它说:

You are correct VLAs are a C99 feature(optional in C11) and the C++ standard does not include this feature although both gcc and clang allow them in C++ as an extension. We can see they are not allowed by going to the draft C++11 standard section 8.3.4 Arrays which says:

D1 [ constant-expressionopt] attribute-specifier-seqopt
     ^^^^^^^^^^^^^^^^^^^^^^

对于 GCC 使用 -pedantic 标志,当您使用延长警告。如果你的目标C ++ 11,那么你也应该指定使用 -std = C ++ 11 。您可以使用 -pedantic-错误打开警告走入误区。如果您在使用编译code -pedantic 您应该看到以下警告:

For both gcc and clang using the -pedantic flag will warn when you are using an extension. If you are targeting C++11 then you should also specify that using -std=c++11. You can use -pedantic-errors to turn the warning into errors. If you compile your code using -pedantic you should see the the following warning:

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

GCC 记录了他们的各种标准,违约和标志的支持强制执行的标准对他们的语言标准,由GCC 页面的支持,并说:

gcc documents their support for various standards, defaults and flags to enforce standard on their Language Standards Supported by GCC page and it says:

要获得所有标准所要求的诊断,你应该
  如果你希望他们能同时指定-pedantic(或者-pedantic-错误
  错误,而不是警告)。

to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

在一般支持什么 GCC 做,但你可以找到他们的语言兼容性

In general clang supports what gcc does but you can find more details on their Language Compatibility page.

请注意由GingerPlusPlus提到 STD:向量被认为是替代的VLA在C ++中。

Note as mentioned by GingerPlusPlus std:vector is considered the alternative for VLA in C++.

这篇关于C ++数组大小声明和const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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