Re: "span"

Liste des GroupesRevenir à cl c  
Sujet : Re: "span"
De : ben.usenet (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.c
Date : 23. Mar 2024, 02:19:02
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <878r29iyvd.fsf@bsb.me.uk>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
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.

...
C already have a syntax for pointer to array
>
int (*)[2] p;

I think you mean

  int (*p)[2];

--
Ben.

Date Sujet#  Auteur
22 Mar 24 * "span"13Stefan Ram
22 Mar 24 +* Re: "span"8Thiago Adams
23 Mar 24 i`* Re: "span"7Ben Bacarisse
23 Mar 24 i `* Re: "span"6Thiago Adams
23 Mar 24 i  `* Re: "span"5Keith Thompson
24 Mar 24 i   `* Re: "span"4Ben Bacarisse
24 Mar 24 i    `* Re: "span"3Keith Thompson
24 Mar 24 i     +- Re: "span"1Ben Bacarisse
25 Mar 24 i     `- Re: "span"1Lawrence D'Oliveiro
23 Mar 24 +* Re: "span"2Ben Bacarisse
23 Mar 24 i`- Re: "span"1Kaz Kylheku
23 Mar 24 +- Re: "span"1fir
23 Mar 24 `- Re: "span"1Blue-Maned_Hawk

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal