Sujet : Re: "span"
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 23. Mar 2024, 15:16:08
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <88f3b4c1-6dad-4fec-8941-fc4e4e755cd1@gmail.com>
References : 1 2 3
User-Agent : Mozilla Thunderbird
Em 3/22/2024 9:19 PM, Ben Bacarisse escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:
On 22/03/2024 08:30, Stefan Ram wrote:
Some people suggested to introduce a feature "span" (probably a
type) into C++, so that when one has an int array "a" and calls
"f( a )", the length of a is being transferred to "f", which "f"
is defined using "void f(span<int> a)". The "span" in "f" then
knows the length of "a". This is supposed to be less error prone
than passing a pointer with a separate length argument.
Of course, I immediately wondered whether one could implement
such a "span" for C, and here's a draft:
#include <stdio.h>
#define SPAN_PARAM(x,a,n) x*a,size_t const n
#define SPAN(a) a,(sizeof a/sizeof 0[a])
void print( SPAN_PARAM( int, a, n ))
{ for( size_t i = 0; i < n; ++i )
printf( "%lld: %d\n", i, a[ i ]); }
int main( void )
{ int a[ 3 ]={ 4, 6, 8 };
print( SPAN(a) ); }
. But since C is another language, there are other forces at work,
which means that the overall usability of such macros in C might not
make their definition and use worthwhile. Of course, the "smart" span
type in C++ surely can do more than my simple macros can do in C!
>
>
(
C++ proposals use new type<T> instead of qualifiers. This creates a big
mess on the type system and incompatibility with C.
I don't have idea how/why they do this constantly.
)
>
C does not need this for parameters since the current array syntax already
cover that.
>
void f(int n, int a[n])
Although you can't rely on this anymore since it's now an optional part
of the language.
I think it is back in c23.
But not vlas.
...
C already have a syntax for pointer to array
>
int (*)[2] p;
I think you mean
int (*p)[2];
yes.