简体   繁体   English

C ++是否支持可变长度数组?

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

No, wait, bear with me... 不,等等,忍受我...

VLAs were always a GCC extension, but they were adopted by C99: VLA始终是GCC扩展,但已被C99采用:

[C99: 6.7.5.2/4]: If the size is not present, the array type is an incomplete type. [C99: 6.7.5.2/4]:如果不存在大小,则数组类型为不完整的类型。 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 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). [C++11: 1.1/2]: C ++是基于ISO / IEC 9899:1999(以下称为C标准)中指定的C编程语言的通用编程语言。 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提供的功能,C ++还提供其他数据类型,类,模板,异常,名称空间,运算符重载,函数名称重载,引用,免费商店管理运算符和其他库功能。

So shouldn't C++11 have VLAs too? 那么C ++ 11也不应该也有VLA吗?

That leeway wording doesn't mean that any and everything in C99 is in C++11. 留有余地的用词并不意味着C99中的所有内容都在C ++ 11中。 What you quoted is just introductory text. 您引用的只是介绍性文字。

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 TD where D has the form [C++11: 8.3.4/1] :在声明TDD的形式为

D1 [ constant-expression opt ] attribute-specifier-seq opt D1 [ 常量表达式opt ] 属性说明符-seq opt

[..] [..]

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中未提及此差异。

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

This compiles for me: (g++ 4.6 with -std=c++0x ). 这为我编译:(g ++ 4.6 with -std=c++0x )。 But it doesn't compile with -pedantic (thanks @MarkB). 但是它不能用-pedantic编译(感谢@MarkB)。 Instead it warns that " template.cpp:7:12: warning: ISO C++ forbids variable length array 'n' [-Wvla] " 而是警告“ template.cpp:7:12:警告: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. 因此,编译器在编译时无法知道n的大小。 Is this a GNU extension to C++? 这是C ++的GNU扩展吗? This does appear to be a GNU extension, and that VLAs are not an official part of C++11. 这似乎是GNU扩展,并且VLA不是C ++ 11的正式组成部分。

(Of course, I'm just playing with a compiler. So take this with a pinch of salt.) (当然,我只是在玩编译器。因此,请花点儿力气。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM