C++ Logo

std-proposals

Advanced search

Re: Fixing C-style arrays

From: J Decker <d3ck0r_at_[hidden]>
Date: Sun, 15 Mar 2020 20:33:05 -0700
On Thu, Mar 12, 2020 at 2:40 PM Maciej Cencora via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hi,
>
> I propose to deprecate in C++23:
> 1) mixed pointer and array comparisons:
> int a[2]; int b[2];
> a == &b[0];
>

Why can't I compare those? They're certainly not equal, the address of the
first element, vs the address of the array?



>
> 2) array decl in func parameters:
> void foo(int a[2]);
>

I think rather than having this removed, that it be strengthened... so if
you pass a thing that's not an int[2] (const address of two integers), the
compiler should notify you.
If the function wants specifically an array of N elements, and then uses
that to condition the limits of loops used in itself, and then gets passed
a much bigger thing...

void foo(int a[2]){ }
int main( void ) { int a[5]; foo(a ); } // should be an error I would
think.

as differently typed as, (minus the struct namespace)
struct x{ int a[2] };
struct y{ int a[5] };

but with an anonymous struct type around them.

And really I think this would be an error too, because it's not a exactly
sized array, but is a pointer to some-thing.
  int main( void ) { int a[2]; int *b = a; foo(b ); }

although a wildcard of any array like

void bar( int a[] )

should still accept an array of any size, or a pointer...



> In order to make C-style arrays behave like other aggregate types in C++26
> or later (supported assignments, comparisons, passing and returning from
> functions)
>
> Regards,
> Maciej
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-03-15 22:36:05