"span"

Liste des GroupesRevenir à l c 
Sujet : "span"
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.c
Date : 22. Mar 2024, 13:30:56
Autres entêtes
Organisation : Stefan Ram
Message-ID : <span-20240322122631@ram.dialup.fu-berlin.de>
  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!

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