🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2203

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

#2203
From: bartc
Date: Fri, 04 Dec 2026 18:03
101 lines
5469 bytes
On 04/12/2026 13:17, David Brown wrote:
> On 04/12/2026 13:07, bartc wrote:
>> On 04/12/2026 08:19, David Brown wrote:

>>> 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)" ?


OK, those are reasonable points. (Although my own preferred solution 
would do away with 'const' entirely. It causes other internal friction 
points besides Generic.)

> 
> 
>> If I run the program below, I get this output with my compiler:
>>
>>           x int
>>           y unsigned int
>>           z const int
>>
> 
> That is non-conforming.

But it can be more useful depending on what you're trying to do, like 
trying to report an actual type together with any attributes.


> (Still, I am impressed that you implemented _Generic at all!)

Actually, it's surprisingly simple - just a 50-line function. (Say 60 if 
constness needs to be ignored.)


> gcc allows "int" and "const int" generic associations because "int" and 
> "const int" are not compatible types, and the constraint in the 
> description of "_Generic" says that no two associations shall have 
> compatible types.  I consider this a flaw in the standard - I think the 
> constraint should also limit duplications where the associations differ 
> only in their type qualifications.  Even better would be to disallow 
> qualified types, array types or function types in the generic 
> associations, since those can never be matched.  clang gives a warning 
> for such unusable generic associations - gcc silently accepts them (even 
> with -Wall -Wextra).  gcc is technically entirely correct, but clang is 
> more helpful.

_Generic is problematic because of C's messy types anyway. For example:

  #define typename(x) (_Generic((x),\
     long:       "long",\
     int64_t:    "int64",\
     default:    "other"))

This compiles under Windows, but fails under Linux because int64_t is an 
alias for 'long' (under Windows it's an alias for 'long long'; this when 
both OSes are 64-bit).

It can also fail where T and U are distinct user-types but one day they 
end up as aliases of the same type. Then they will clash in any _Generic 
expression.

Technical Details
Message-ID: <11euv9l$1rult$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!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-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: bartc <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Iterators over slices and views and ranges analogs in C
Date: Fri, 4 Dec 2026 18:03:32 +0000
Organization: A noiseless patient Spider
Lines: 101
Message-ID: <11euv9l$1rult$1@dont-email.me>
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; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 04 Dec 2026 18:03:33 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1964733"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+gOFmLzf1j49tjoFW6nKN3"; posting-host="b48d6065a37b4eb51257cf01ab926585"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:EJLlK430X8tPCl0kKQvxkbZ+ASI= sha256:Gaeth6PLVvYHdwWH6uivpvDFtcXABC6YBgPkEEFMIMY=
	sha1:SUDhLhPOyqFQb8jgHoZJ7e2rrg0= sha256:eqwJBEXpARFJ7QBJSK299s+unJaz9ejltbh8n2G7dig=
Content-Language: en-GB
In-Reply-To: <11euegk$1is8o$1@dont-email.me>