Article View: comp.lang.c
Article #2174Re: Iterators over slices and views and ranges analogs in C
From: David Brown
Date: Fri, 04 Dec 2026 13:17
Date: Fri, 04 Dec 2026 13:17
152 lines
7039 bytes
7039 bytes
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)" ?
> 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.
> With gcc, it produces:
>
> x int
> y unsigned int
> z int
That output is correct.
>
> It treats 'const int' the same as 'int', but it doesn't complain that
> there are duplicate types in the list, as happens if I were to add a
> line like this to the macro:
>
> int: "int2",\
>
> So what's happening, and how does my compiler manage to do what it
> expected?
Your compiler does not do what is expected - at least, not what is
expected by someone reading the C standards (I can't answer for what you
personally, or anyone else, might expect).
(Still, I am impressed that you implemented _Generic at all!)
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.
>
>
>> or between an array type and a pointer to an element of the array.
>> (It /can/ distinguish between "int" and "long", or "long" and "long
>> long", even if they are the same size - it distinguishes between
>> incompatible types.)
>
> So 'int' and 'const int' are compatible? If I use int* and const int*,
> now those are distinct types according to gcc!
Sorry, I was imprecise, and that was not helpful! "int" and "const int"
are not compatible, but the after lvalue conversion the top level
qualifiers are dropped. _Generic distinguishes between types that have
incompatible types after lvalue conversion, array to pointer conversion,
and function to pointer conversion.
>
> -------------------------
> #include <stdio.h>
> #include <stdint.h>
>
> #define typename(x) (_Generic((x),\
> int: "int",\
> unsigned int: "unsigned int",\
> const int: "const int",\
> default: "other"))
>
> #define showtype(x) printf("%10s %s\n",#x, typename(x))
>
> int main(void) {
> int x;
> unsigned int y;
> const int z;
>
> showtype(x);
> showtype(y);
> showtype(z);
> }
>
>
Technical Details
Message-ID:
<11euegk$1is8o$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<-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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.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: Fri, 4 Dec 2026 14:17:08 +0100 Organization: A noiseless patient Spider Lines: 152 Message-ID: <11euegk$1is8o$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 04 Dec 2026 13:17:09 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1667352"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4xK1SZeWFWhTaH5SSWqdL2T8jO7ixAsw="; posting-host="f8d429fc41b298b14a9203a87d5618c6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:o+DeTAYEchzRM20p/fcu0X+OWuc= sha256:oZ1Ka5wcu5rnzTD8pBjjwrAvP4k9U2T8gPkKmNIOO2I= sha1:tsvitvRMRSVmmBiv9S8nTESoVJI= sha256:wNxXeiHuwVerwEiK7DrPp7KscaTKMtITecrmLE3PBM0= In-Reply-To: <11euadh$1gp4t$1@dont-email.me> Content-Language: en-GB, nb-NO