Re: "span"

Liste des GroupesRevenir à cl c  
Sujet : Re: "span"
De : bluemanedhawk (at) *nospam* invalid.invalid (Blue-Maned_Hawk)
Groupes : comp.lang.c
Date : 23. Mar 2024, 22:35:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <pan$4ba47$20b73398$89c6683d$d9bb1b67@invalid.invalid>
References : 1
User-Agent : Pan/0.154 (Izium; 517acf4)
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!

You managed to inspire me to try my own hand at coming up with some macros
for spannage in C.  By means of the library of macros P99 and the power of
C23, i ended up with this:

#define span(...) typeof (__VA_ARGS__) *
#define span_of(...) (((struct {const size_t length; typeof (__VA_ARGS__)
members
#define with_members(...) [P99_NARG(__VA_ARGS__)];})
{P99_NARG(__VA_ARGS__), {__VA_ARGS__}}).members)
#define span_length(...) (((struct {const size_t length; typeof
(__VA_ARGS__[0]) members[];} *)&(((char *)(__VA_ARGS__))[-sizeof
(size_t)]))->length)

This is used as such:

void f(const span(int) obj)
{
printf("%d, %d, %d, %zu", obj[1], obj[2], obj[5],
span_length(obj));
}

int main(void)
{
span(int) the_span = span_of(int) with_members(1,2,3,4,5,6);
printf("%d, %d, %d, %zu", the_span[1], the_span[2], the_span[5],
span_length(the_span));
return 0;
}

Now, you may notice that this, uh. doesn't actually work:  the
with_members macro breaks if you're initializing the array with members
that have embedded commas in them.  I tried to fix this, but my compiler
claimed that P99's P99_REMOVE_PAREN macro doesn't exist even though it
does.

On a tangent, the way that the typeof operator works in C23 means that
macros no longer have to worry about the fact that declarations such as
int(*)(int) x;
and
int[3] y;
don't work, meaning that they can accept any typename and use it raw
without needing the invoker to make a typedef.

--
Blue-Maned_Hawk│shortens to Hawk│/blu.mɛin.dʰak/│he/him/his/himself/Mr.
blue-maned_hawk.srht.site
The dark side of the C preprocessor is a path to many abilities, some
considered unnatural.

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