C ++可变前pressions定义数组的大小? [英] C++ Variable expressions for defining array size?

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

问题描述

我在C ++对于新手,​​我练我的编码学校的电脑上,所以我必须使用在线编译器(www.CompileOnline.com)。我具有由一个函数定义,然后被用于初始化一个数组,像这样的变量:

I'm a newb at C++ and I practice coding on my school computer, so I have to use an online compiler (www.CompileOnline.com). I have a variable that is defined by a function and is then used to initialize an array like so:

INT VAR =函数();

int var = function(a);

INT NUM [VAR];

int num[var];

这code工作在网站上就好了,但在Visual Studio中前preSS 2012年,给我一个错误:

This code works just fine on the website, but on Visual Studio Express 2012 it gives me an error:

C2057:预期不断前pression

C2057: expected constant expression

这是与Visual Studio的一个问题?我读过,这是一个C ++的规则,但为什么它在网站上工作吗?任何帮助是AP preciated,谢谢。

Is this a problem with Visual Studio? I've read that it's a C++ rule, but then why does it work on the website? Any help is appreciated, thanks.

推荐答案

这在code段要求被称为的变长数组(沃拉斯)。在C或C ++语言的这一功能的支持取决于编译器和标准的版本。

The feature that the code snippet requires is called variable length arrays (VLAs). Support for this feature in the C or C++ language depends on the compiler and the version of the standard.


  • C99支持沃拉斯作为标准配置。

  • 版本低于C99(包括C90)更早版本不支持沃拉斯的标准,但有些编译器可以实现它作为语言的扩展。

  • C11使得沃拉斯的的可选的功能。

  • C ++ 14支持沃拉斯的限制变种,叫的动态数组

  • 版本早于C ++ 14(包括C ++ 11,C ++ 03和C ++ 98)不支持沃拉斯的标准,但有些编译器可以实现它作为一个扩展。

在特别 GCC实现沃拉斯作为语言扩展C90和C ++ ,显然www.compileonline.com使用GCC作为编译器(4.7.2版本撰写本文时)。在Visual C ++编译器的版本号沃拉斯实施

In particular, GCC implements VLAs as a language extension for C90 and C++, and apparently www.compileonline.com uses GCC as the compiler (version 4.7.2 as of this writing). No version of the Visual C++ compiler implement VLAs.

香草萨特谈起C ++ 14的动态数组功能

在语言,草案C ++ 14现在允许基于堆栈的数组在运行时确定的尺寸:

In the language, draft C++14 now allows stack-based arrays to have a size determined at run time:

void f(std::size_t n)
{
   int a[n];

   ...

}

请注意,这是不一样的C99可变长度的数组(VLA),
  并且C11标准作出VLAS有条件地支持这样
  他们是在一个标准的C需要移植的C中不再
  编译器。特别是,C ++不明确不支持以下
  从C99沃拉斯功能哪些C ++摸上去都是不可取的:

Note that this is not the same as C99 variable length arrays (VLAs), and that the C11 standard has made VLAs conditionally-supported so that they are no longer part of portable C required in a conforming C compiler. In particular, C++ explicitly not does support the following features from C99 VLAs which C++ feels are not desirable:


      
  • 多维数组,其中除顶层有一个运行时
      界(类似于,新的前pressions数组形式不支持
      要么)

  •   
  • 修改函数声明语法

  •   
  • 的sizeof(A)是一个运行评估前pression返回的大小 A

  •   
  • 的typedef int类型的[N]; 评估 N 和传球穿过的typedef

  •   
  • multidimensional arrays, where other than the top level has a runtime bound (in analogy, the array form of new expressions doesn’t support that either)
  • modifications to the function declarator syntax
  • sizeof(a) being a runtime-evaluated expression returning the size of a
  • typedef int a[n]; evaluating n and passing that through the typedef

如果您希望C ++ code,在pretty C ++的多任何版本的作品,可以考虑使用的 的std ::矢量 来代替:

If you want C++ code that works in pretty much any version of C++, consider using std::vector instead:

#include <vector>

int main()
{
    int var = function(a); // Assume function() has been defined.
    std::vector<int> num(var); // Creates a vector with var number of elements.
    // ...
    int num1 = num[1]; // You can access elements in vectors just like arrays.
    num[1] += 10;
    // ...
}

这篇关于C ++可变前pressions定义数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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