🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2222

Re: Iterators over slices and views and ranges analogs in C

#2222
From: Waldek Hebisch
Date: Sat, 05 Dec 2026 02:51
81 lines
4713 bytes
David Brown <david.brown@hesbynett.no> wrote:
> On 04/12/2026 13:07, bartc wrote:
>> On 04/12/2026 08:19, David Brown wrote:
>>> On 04/12/2026 01:59, Kenny McCormack wrote:
>>>> In article <11esd12$mfam$1@raubtier-asyl.eternal-september.org>,
>>>> Bonita Montero  <Bonita.Montero@gmail.com> wrote:
>>>>> Better use C++ for what you want to do.
>>>>
>>>> Or Fortran.
>>>>
>>>> Or 8086 assembler.
>>>>
>>>> All better choices.
>>>
>>> Actually, his best choice would be to write the question in 
>>> comprehensible English and we could start from there.
>>>
>>> It looks like he has questions about _Generic, and possibly about the 
>>> use of array types in the controlling expression.  That could easily 
>>> turn into a thread that is both on-topic and interesting here - 
>>> _Generic is probably somewhat poorly understood and underused by many 
>>> C programmers.
>>>
>>> For the record, the controlling expression in a _Generic selection 
>>> undergoes lvalue conversion and array to pointer conversion - just 
>>> like when you pass things to a function.  So it cannot distinguish 
>>> between an "int" and a "const int",
>> 
>> Why not? 
> 
> I explained why not - the _Generic selection controlling expression 
> undergoes lvalue conversion and array to pointer conversion (and also 
> function type to function pointer conversion).  It matches on compatible 
> types, ignoring top-level qualifiers.  (The converted lvalue is never 
> evaluated, so it does not actually have to refer to a real object.  You 
> can write "int xs[2];" and then have "_Generic(xs[27], ..." if you like.)
> 
> Or are you asking why _Generic is define that way?  I think that is best 
> seen by thinking about how it could be different.  Let's suppose you 
> want a generic "print" macro that will handle int, long, float, and 
> double types.  You have your functions :
> 
>        void print_int(int x);
>        void print_long(long x);
>        void print_float(float x);
>        void print_double(double x);
> 
> and your type-generic macro :
> 
>        #define print(x) (_Generic((x), \
>                int : print_int, \
>                long : print_long, \
>                float : print_float, \
>                double : print_double)(x)
> 
> Do you really want to add lines for "const int", "volatile int", 
> "_Atomic int", and all combinations thereof?  That would multiply your 
> _Generic matches by 8.  And if you had them, how should they be matched? 
>  It may seem obvious that if you have :
> 
>        int i;
>        const int ci;
> 
> then "print(i)" should match "int" while "print(ci)" should match "const 
> int".  But what about "print(42)" ?  Or "print(i + ci)" ?

AFAICS both your specific points have reasonable solution.  Matching
could be two phase, in first phase aiming for match including
qualifiers.  If that works, this is the result.  If not drop
qualifires and try again.  That way currently standard code
would work as before, but you could make more fine grained
selection when needed.  Concerning '42' I would think that
only reasonable way to make lvalue from it would add 'const'
to the type.  Similarly for expressions.  Of course, my
notion of reasonable seem to be in conflict with C standard
notion of reasonable.  If they put something in the standard
that forces/strongly supports different choice, then you can
go with their choice.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11evu6m$ja7$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder8.news.weretis.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com> <11esd12$mfam$1@raubtier-asyl.eternal-september.org> <11et39b$rj55$1@news.xmission.com> <11ett3b$1a41p$1@dont-email.me> <11euadh$1gp4t$1@dont-email.me> <11euegk$1is8o$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder8.news.weretis.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Iterators over slices and views and ranges analogs in C
Date: Sat, 5 Dec 2026 02:51:04 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11evu6m$ja7$1@paganini.bofh.team>
References: <-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com> <11esd12$mfam$1@raubtier-asyl.eternal-september.org> <11et39b$rj55$1@news.xmission.com> <11ett3b$1a41p$1@dont-email.me> <11euadh$1gp4t$1@dont-email.me> <11euegk$1is8o$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 5 Dec 2026 02:51:04 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="19783"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2