C++ Logo

std-proposals

Advanced search

Arrays (VLAs) as function parameters (as in C99)

From: Alejandro Colomar (man-pages) <"Alejandro>
Date: Sat, 6 Nov 2021 22:31:56 +0100
Hi,

I have a new proposal to discuss a new core language feature to be added
to C++ (already existing in C99).

C2X added a new principle for ISO C:

[
15. Application Programming Interfaces (APIs) should be self-documenting
when possible. In particular, the order of parameters in function
declarations should be arranged such that the size of an array appears
before the array. The purpose is to allow Variable-Length Array (VLA)
notation to be used. This not only makes the code's purpose clearer to
human readers, but also makes static analysis easier. Any new APIs added
to the Standard should take this into consideration.
]

In my experience writing C & C++ compatible headers, the most difficult
problem which could not be solved by preprocessor macros is this one.
Either you give up in C and use pointer syntax, giving up also on the
self-documentation that VLA syntax would have provided, or you write a
different prototype for C and C++, which also increases maintenance costs.

True VLAs are very dangerous, since they can silently cause stack
overflows, and so they were not added to C++, and were made optional in
ISO C.
However, VLA syntax for function parameters does not suffer from those
issues: it is just syntactic sugar for pointer parameters.

My proposal is to add VLA syntax to C++, to add compatibility with C
headers, and also to have the same self-documentation that C prototypes
already have.

Thanks,

Alex


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

Received on 2021-11-06 16:32:01