c++支持可变长度数组吗?

[英]Does C++ support Variable Length Arrays?


No, wait, bear with me...

不,等等,容忍我……

VLAs were always a GCC extension, but they were adopted by C99:

VLAs一直是GCC的扩展,但C99采用了VLAs:

[C99: 6.7.5.2/4]: 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; 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.

[C99: 6.7.5.2/4]:如果不存在大小,则数组类型为不完整类型。如果大小为*而不是表达式,则数组类型为未指定大小的可变长度数组类型,只能用于函数原型范围的声明;这样的数组仍然是完整的类型。如果大小是一个整数常量表达式,而元素类型具有已知的常量大小,则数组类型不是可变长度的数组类型;否则,数组类型是可变长度数组类型。

C99 is also known as ISO/IEC 9899:1999.

C99也被称为ISO/IEC 9899:1999。

Now:

现在:

[C++11: 1.1/2]: C++ is a general purpose programming language based on the C programming language as specified in ISO/IEC 9899:1999 (hereinafter referred to as the C standard). In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

c++是基于ISO/IEC 9899:1999(以下简称C标准)中规定的C语言编程的通用语言。除了C提供的功能之外,c++还提供了其他数据类型、类、模板、异常、名称空间、操作符重载、函数名重载、引用、免费存储管理操作符和其他库功能。

So shouldn't C++11 have VLAs too?

那么c++ 11不是也应该有VLAs吗?

4 个解决方案

#1


15  

That leeway wording doesn't mean that any and everything in C99 is in C++11. What you quoted is just introductory text.

这种灵活的措辞并不意味着C99中的所有内容都是在c++ 11中。你所引用的只是引言。

#2


13  

This C99 feature is effectively overridden by C++'s own semantics, as can be any otherwise "inherited" feature:

这个C99特性被c++自身的语义所覆盖,可以是任何其他“继承”特性:

[C++11: 8.3.4/1]: In a declaration T D where D has the form

[C+ 11: 8.3.4/1]:在声明T D中,D有表格

D1 [ constant-expressionopt ] attribute-specifier-seqopt

D1 [constant-expressionopt]属性-specifier-seqopt

[..]

[. .]

This is the only array declaration syntax we're given in C++.

这是c++中唯一给出的数组声明语法。

Note that no mention of this difference is given in the "compatibility with C" clause C.1.

注意,在“与C兼容”条款C.1中没有提到这种差异。

#3


7  

The definition of constant-expression is different for the two languages.

对于两种语言来说,常量表达式的定义是不同的。

const size_t size = 5;
int array[size]; // array in C++, VLA in C

#4


2  

This compiles for me: (g++ 4.6 with -std=c++0x). But it doesn't compile with -pedantic (thanks @MarkB). Instead it warns that "template.cpp:7:12: warning: ISO C++ forbids variable length array ‘n’ [-Wvla]"

这为我编译:(g+ 4.6与-std=c++0x)。但是它没有使用-pedantic编译(谢谢@MarkB)。相反,它警告“模板”。警告:ISO c++禁止可变长度数组' n ' [-Wvla]"

int main(int argc, char ** argv) {
    int n[argc];
}

So the size of n can not be known at compile time by the compiler. Is this a GNU extension to C++? This does appear to be a GNU extension, and that VLAs are not an official part of C++11.

所以编译器在编译时不能知道n的大小。这是c++的GNU扩展吗?这似乎是一个GNU扩展,并且VLAs不是c++ 11的官方部分。

(Of course, I'm just playing with a compiler. So take this with a pinch of salt.)

当然,我只是在玩编译器。所以,我们还是少吃一点吧。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/12/21/9479d4ce89254866f333e2e4f80e8c24.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告