I am often asked questions about why a particular feature, present in standard C, hasn’t become part of the C++ standard. This time, I’ll discuss why C++ (as of the C++17 standard) doesn’t include the variable length arrays (VLAs) feature that has been present in the C standards since C99.
Keep in mind that C has never been a proper subset of C++, contrary to what you might have read or heard, and likely never will be. The two languages have been evolving along different paths for quite some time now. Just because a feature pops up in C doesn’t mean it will necessarily ever appear in C++. Also, remember that just because a feature is part of a standard, it won’t necessarily be implemented by all compiler vendors. There are several C99 features, for example, that some compiler implementations do not yet support, and might never support.
That said, let’s examine the question of support for variable length arrays in C++.
The first C++ standard (C++98) arrived before the C99 standard, the first C standard that included variable length arrays (VLAs). By the time C++03 arrived in 2003, it was already evident that C VLAs were not considered a highly-used, universally-implemented, mainstream feature, and there was already talk of making the VLA feature optional in the next C standard.
Indeed, in the C11 standard, the VLA feature was demoted to an optional feature, so it’s no longer required by compliant compiler implementations. A conditional feature macro in C11, named __STDC_NO_VLA__, gives your code a way to test whether or not VLAs are supported in your compiler implementation. A value of 1 indicates no support for VLAs is present.
Some C++ compilers support VLAs as a non-standard extension, primarily to aid in migrating C code that uses VLAs into C++. But VLAs are not present in the C++ standards, even as an optional feature.
In all of the many projects I’ve worked on in C, we have explicitly avoided the use of VLAs, because even though the feature appeared in the C99 standard, it is not supported in some of the compiler implementations we have used. We thus considered the feature non-portable, even though it was part of the C99 standard. In many cases, the local coding standard for the project explicitly stated that VLAs shall not be used, because of the portability issue. Because I emphasize portable code when teaching C programming courses, I sometimes don’t include VLAs, beyond just mentioning them.
C++ doesn’t really need VLAs. Using the STL’s vector, you can easily create an array-esque data structure whose length is based on the value of a variable or parameter at run time, with the added benefit of being able to change its size later on. The only reason to consider adding VLAs to C++ would be for compatibility with C. But many C implementations never supported VLAs, and now that the feature is optional (in C11), it’s even less likely that a C implementation will support it.
Thus, there is no compelling reason to add VLAs to the C++ standard.