🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2171

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

#2171
From: bartc
Date: Fri, 04 Dec 2026 12:07
77 lines
3793 bytes
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? If I run the program below, I get this output with my compiler:

          x int
          y unsigned int
          z const int

With gcc, it produces:

          x int
          y unsigned int
          z int

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?


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

-------------------------
  #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: <11euadh$1gp4t$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>

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 12:07:12 +0000
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <11euadh$1gp4t$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 04 Dec 2026 12:07:14 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1598621"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/hOKm1L95Fc/+kemxQ9fph"; posting-host="b48d6065a37b4eb51257cf01ab926585"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:u/5eLBVRI0IYSLJg+XHgEvnAe7s= sha256:w99s7vM2bO2fe6hq8VLDEePIFDqA/4smxkSDWTdgSxs=
	sha1:+VgjYZi6U2k1GUiuz7YZhvhvyuY= sha256:7qPQiIha20IhrFwLMHcErGO0pw/EYf0IjwUnq2CucKQ=
In-Reply-To: <11ett3b$1a41p$1@dont-email.me>
Content-Language: en-GB