Deutsch English Français Italiano |
<span-20240322122631@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.c Subject: "span" Date: 22 Mar 2024 11:30:56 GMT Organization: Stefan Ram Lines: 27 Expires: 1 Feb 2025 11:59:58 GMT Message-ID: <span-20240322122631@ram.dialup.fu-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de GJsj7qnSwEYTNfaDR1QHAg0w6nC0iC49HqiACfMATgJFYp Cancel-Lock: sha1:xXpEut2ZE64CxeCmEFbSy8M+xy0= sha256:3pG9ilK7VPJPHFSBgAQ9gMHcSYk7M6aepfySdcwCfJI= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Accept-Language: de-DE-1901, en-US, it, fr-FR Bytes: 2387 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!