C++ Logo

std-proposals

Advanced search

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

From: Alejandro Colomar (man-pages) <"Alejandro>
Date: Fri, 12 Nov 2021 22:30:26 +0100
Hi Thiago,

On 11/7/21 02:58, Thiago Macieira via Std-Proposals wrote:
> On Saturday, 6 November 2021 14:31:56 PDT Alejandro Colomar (man-pages) via
> Std-Proposals wrote:
>> In my experience writing C & C++ compatible headers, the most difficult
>> problem which could not be solved by preprocessor macros is this one.
>
> Why not?
>
> #if __STDC_VERSION__ >= 199901L
> # define size_at_least(n) static n
> # define size_exactly(n) static restrict n
> #else
> # define size_at_least(n)
> # define size_exactly(n)
> #endif
>
> void process_array(size_t count, char payload[size_at_least(count)]);

I didn't think of it that way, but yes, it makes sense.
I only thought about making C syntax (without macros hiding the stuff)
compile in C++.

BTW, 'static restrict n' doesn't mean size_exactly() unless I'm also a
bit blind here. But yes, that concept makes sense.

But having C++ accept pure C syntax would be nicer.

>
> > 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.
>
> Ok, there's a clear motivation here. Write the paper.
>
> I suspect the C++ side will be "syntax accepted but ignored". Compilers can
> choose to print diagnostics if they want to, of course.

That's already what C compilers do, AFAIK. There's no requirement from
the standard to diagnose that, and no compiler that I know diagnoses it,
unless they changed it recently.

I'll learn more about it while I write a paper.

Thanks,
Alex


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

Received on 2021-11-12 15:30:31