Can I use a C Variable Length Array in C++03 and C++11?

会有一股神秘感。 提交于 2019-12-01 22:24:45

VLAs are not in standard C++03 or C++11, so you'll better avoid them if you want to write strictly standard conforming code (and use e.g. std::vector, which generally use heap for its data - but you might use your own allocator...).

However, several C++ compilers (recent GCC & Clang) are accepting VLAs as an extension.

It is the same for flexible array members; they are not standard in C++ (only in C) but some compilers accept them.

dynarray-s did not get into the C++11 standard...

Not if you want code that is standard C++.

No C++ standard supports VLAs, but some C++ compilers do as a vendor-specific extension.

You can achieve a similar effect in C++ using the standard vector. Note that, unlike VLAs which can only be sized when created, a standard vector can be resized as needed (subject to performing appropriate operations on it).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!