🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2246

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

#2246
From: David Brown
Date: Sat, 05 Dec 2026 12:31
148 lines
7151 bytes
On 04/12/2026 19:03, bartc wrote:
> 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.)
> 

I think some kind of "const" is an extremely helpful concept in a 
programming language - I think the ideal is for things to be as "const" 
as possible by default.  But I don't think the way "const" works in C 
(or C++, for that matter) is as good as it could have been.  My 
preference would be a clear distinction between "this is something that 
will never change", and "I do not intend to change this thing here" - 
with that promise being enforced by the language, and therefore assumed 
to hold for optimising purposes.

>>
>>
>>> 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.
> 

I agree that your interpretation could sometimes be very useful, but it 
is not conforming and not what other C programmers would expect. 
Perhaps you could change your _Generic to be fully conforming, and then 
add an extension with a different name, with your own semantics - then 
both would be available.  Or implement the expected C2y enhancements to 
_Generic - there "_Generic(typeof(x))" will work very much as your 
current "_Generic(x)".

> 
>> (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 is not problematic if you stick to a consistent pattern.  Choose either :

	_Generic(x,
		signed char : ...
		short : ...
		int : ...
		long : ...

or

	_Generic(x,
		int8_t : ..
		int16_t : ...
		int32_t : ...
		int64_t : ...

Less mixing will be fewer problems!

> 
> 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.
> 

That would always cause challenges - you will never be able to 
distinguish aliases.  If you could, they would be different types, not 
aliases!



Technical Details
Message-ID: <11f107p$2okjb$6@dont-email.me>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.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> <11euv9l$1rult$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!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Iterators over slices and views and ranges analogs in C
Date: Sat, 5 Dec 2026 13:31:53 +0100
Organization: A noiseless patient Spider
Lines: 148
Message-ID: <11f107p$2okjb$6@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>
 <11euv9l$1rult$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 05 Dec 2026 12:31:54 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2904683"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+iQMkHH7pt6U7r4mWdiVQQ58YKW3ibNI8="; posting-host="e6744434c7b28607c9aa71bc7804445d"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gGh0SsQz8OlZ6OdHKbOCkSC9BKs= sha256:Zwkdhj9VULn2FMPSGJ1kSpQunx75wTNbnEPzgfsfC+o=
	sha1:FKmVd0KAHwI0QfaIevPU8InvWRQ= sha256:vieTyMT41kTcoE8GQAW+74CO4kq1oVUnpOg11Lx9+Og=
In-Reply-To: <11euv9l$1rult$1@dont-email.me>
Content-Language: en-GB