目录
一:C++数组长度必须是编译时已知?
C++ Primer 5th Edition 101页提到:数组长度必须是编译时已知的。
但,实际上,默认情况下,g++编译器支持变长数组(VLA, variable length array)。
二:g++默认支持变长数组
代码如下:
#include<iostream>
using namespace std;
int main() {
int a;
cin >> a;
int iarr[a];
// a 输入 2
for (int i = 0; i < a; ++i) {
iarr[i] = i + 10;
}
// 输出为:10, 11
for (int i = 0; i < a; ++i) {
cout << iarr[i] << endl;
}
return 0;
}
编译器版本 gcc version 11.2.0,具体如下:
marvin@vm:~/eden$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.2.0-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
marvin@vm:~/eden$
编译执行都没有问题
marvin@vm:~/eden$ g++ test_vla.cpp -o a
marvin@vm:~/eden$ ./a
2
10
11
marvin@vm:~/eden$
三:g++通过编译选项控制是否支持变长数组
By 2022.6.29,最新的C++ standard是C++23,C++标准依然 *不支持* 变长数组(VLA, variable length array)。注意,这里说的是C++ standard。
marvin@vm:~/eden$ g++ test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++11 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++17 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++14 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++17 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++20 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++23 test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -std=c++24 test_vla.cpp -o a
g++: error: unrecognized command-line option ‘-std=c++24’; did you mean ‘-std=c++14’?
marvin@vm:~/eden$
marvin@vm:~/eden$
C++ standard 不支持VLA,但,编译器扩展是可以支持 VLA 的,比如g++编译器就支持了 VLA,而且是默认开启的。
1. g++默认开启 VLA 的编译器扩展功能,因此g++默认支持 VLA
2. 使用选项 -pedantic 则 VLA 编译告警
3. 使用选项 -pedantic-errors 则 VLA 编译失败
marvin@vm:~/eden$ g++ test_vla.cpp -o a
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -pedantic test_vla.cpp -o a
test_vla.cpp: In function ‘int main()’:
test_vla.cpp:9:9: warning: ISO C++ forbids variable length array ‘iarr’ [-Wvla]
9 | int iarr[a];
| ^~~~
marvin@vm:~/eden$
marvin@vm:~/eden$ g++ -pedantic-errors test_vla.cpp -o a
test_vla.cpp: In function ‘int main()’:
test_vla.cpp:9:9: error: ISO C++ forbids variable length array ‘iarr’ [-Wvla]
9 | int iarr[a];
| ^~~~
marvin@vm:~/eden$
marvin@vm:~/eden$
四:History of C++ standard
Summary Table of history of various C++ versions:
Version | Release Date | Major changes |
---|---|---|
C++98 (ISO/IEC 14882:1998) | October 1998 | The first version |
C++03 (ISO/IEC 14882:2003) | February 2003 | Introduction of value initialization. |
C++11 | August 2011 | Introduction of Lambda Expressions, Delegating Constructors, Uniform Initialization Syntax, nullptr, Automatic Type Deduction and decltype, Rvalue References etc. |
C++14 | August 2014 | Introduction of polymorphic lambdas, digit separators, generalized lambda capture, variable templates, binary integer literals, quoted strings etc. |
C++17 | December 2017 | Introduction of fold expressions, hexadecimal floating point literals, a u8 character literal, selection statements with initializer, inline variables etc. |
C++20 | March 2020 | This update extends C++ with the facilities to inspect program entities such as variables, enumerations, classes and their members, lambdas and their captures, etc. |
C++23 | Future Release | The next major revision of the C++ standard |
以上图片和表格来自:History of C++ - GeeksforGeeks
五:其他材料
gcc对C++ standard的支持
C++ Standards Support in GCC- GNU Project
g++小抄
https://bytes.usc.edu/cs104/wiki/gcc/
g++编译常用指令_return you的博客-CSDN博客_g++ 编译命令
GCC与gcc,g++区别
GCC与gcc,g++区别_程序小人生的博客-CSDN博客_gcc与g++的区别
完