public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ with VLA
@ 2024-03-30  6:10 NightStrike
  2024-03-30  9:05 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: NightStrike @ 2024-03-30  6:10 UTC (permalink / raw)
  To: gcc-help

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

That doc states that VLAs are supported in C++ as an extension.
Compiling the following:

void f(int a, int b[a]);
void f() {
  int c[2];
  f(2, c);
}

with g++ -std=gnu++20 results in the error: "use of parameter outside
function body before ']' token".  The docs say nothing about partial
support of VLA, just that they can be used.

Where this is useful as an extension is in the somewhat reasonable
case of including a C header in a C++ program.  If that C header
declares a function using a C99 VLA, it would be awesome if g++ were
to accept it in -std=gnu++ mode (and it would be fine if it were
rejected in -std=c++ mode).  Consider a situation where you cannot
modify the header, and so have to use it as-is.

Is there perhaps another option to achieve the intended behavior?

If it's not possible, can this be added?

If the consensus is that it shouldn't be added, could the docs at
least clarify what is supported and what is not in C++?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: g++ with VLA
  2024-03-30  6:10 g++ with VLA NightStrike
@ 2024-03-30  9:05 ` Jonathan Wakely
  2024-03-30 18:39   ` NightStrike
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2024-03-30  9:05 UTC (permalink / raw)
  To: NightStrike; +Cc: gcc-help

On Sat, 30 Mar 2024 at 06:12, NightStrike via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
>
> That doc states that VLAs are supported in C++ as an extension.
> Compiling the following:
>
> void f(int a, int b[a]);
> void f() {
>   int c[2];
>   f(2, c);
> }
>
> with g++ -std=gnu++20 results in the error: "use of parameter outside
> function body before ']' token".  The docs say nothing about partial
> support of VLA, just that they can be used.

I don't think GNU C++ has any changes to the function declarator
syntax to support variably modified types. You can define a VLA
object, but you can't use earlier function parameters later in the
function-parameter-list, and you can't use int b[*] in a parameter
either.


>
> Where this is useful as an extension is in the somewhat reasonable
> case of including a C header in a C++ program.  If that C header
> declares a function using a C99 VLA, it would be awesome if g++ were
> to accept it in -std=gnu++ mode (and it would be fine if it were
> rejected in -std=c++ mode).  Consider a situation where you cannot
> modify the header, and so have to use it as-is.
>
> Is there perhaps another option to achieve the intended behavior?
>
> If it's not possible, can this be added?
>
> If the consensus is that it shouldn't be added, could the docs at
> least clarify what is supported and what is not in C++?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: g++ with VLA
  2024-03-30  9:05 ` Jonathan Wakely
@ 2024-03-30 18:39   ` NightStrike
  2024-03-30 18:46     ` NightStrike
  0 siblings, 1 reply; 4+ messages in thread
From: NightStrike @ 2024-03-30 18:39 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

On Sat, Mar 30, 2024 at 5:05 AM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Sat, 30 Mar 2024 at 06:12, NightStrike via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> >
> > https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
> >
> > That doc states that VLAs are supported in C++ as an extension.
> > Compiling the following:
> >
> > void f(int a, int b[a]);
> > void f() {
> >   int c[2];
> >   f(2, c);
> > }
> >
> > with g++ -std=gnu++20 results in the error: "use of parameter outside
> > function body before ']' token".  The docs say nothing about partial
> > support of VLA, just that they can be used.
>
> I don't think GNU C++ has any changes to the function declarator
> syntax to support variably modified types. You can define a VLA
> object, but you can't use earlier function parameters later in the
> function-parameter-list, and you can't use int b[*] in a parameter
> either.

It looks like clang added support for this in v12.  Maybe it would be
a good candidate to add support?  I guess I can file the PR and see
where it lands.

> > Where this is useful as an extension is in the somewhat reasonable
> > case of including a C header in a C++ program.  If that C header
> > declares a function using a C99 VLA, it would be awesome if g++ were
> > to accept it in -std=gnu++ mode (and it would be fine if it were
> > rejected in -std=c++ mode).  Consider a situation where you cannot
> > modify the header, and so have to use it as-is.
> >
> > Is there perhaps another option to achieve the intended behavior?
> >
> > If it's not possible, can this be added?
> >
> > If the consensus is that it shouldn't be added, could the docs at
> > least clarify what is supported and what is not in C++?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: g++ with VLA
  2024-03-30 18:39   ` NightStrike
@ 2024-03-30 18:46     ` NightStrike
  0 siblings, 0 replies; 4+ messages in thread
From: NightStrike @ 2024-03-30 18:46 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

On Sat, Mar 30, 2024 at 2:39 PM NightStrike <nightstrike@gmail.com> wrote:
> It looks like clang added support for this in v12.  Maybe it would be
> a good candidate to add support?  I guess I can file the PR and see
> where it lands.

Just to close the loop for the mailing list archive:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114534

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-30 18:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-30  6:10 g++ with VLA NightStrike
2024-03-30  9:05 ` Jonathan Wakely
2024-03-30 18:39   ` NightStrike
2024-03-30 18:46     ` NightStrike

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).