🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2252

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

#2252
From: David Brown
Date: Sat, 05 Dec 2026 15:43
129 lines
6996 bytes
On 05/12/2026 15:36, Janis Papanagnou wrote:
> On 2026-12-04 15:38, David Brown wrote:
>> On 04/12/2026 14:58, Janis Papanagnou wrote:
>>> On 2026-12-04 13:45, Bonita Montero wrote:
>>>> Am 04.12.2026 um 13:07 schrieb bartc:
>>>>
>>>>>   #define typename(x) (_Generic((x),\
>>>>>       int:                   "int",\
>>>>>       unsigned int:          "unsigned int",\
>>>>>       const int:             "const int",\
>>>>>       default:               "other"))
>>>
>>>> Compared to C++ generics this really, really sucks !
>>>
>>> Grafting features to a language that wasn't designed to support
>>> those regularly often lead to clumsy, ugly, inconsistent designs.
>>> So while it's not surprising, but it also won't change what "C"
>>> actually is (and what's discussed here, the C-language).
>>
>> I don't think _Generics are clumsy, ugly or inconsistent - they are 
>> actually quite clear and simple.  But they do get bulky if you want to 
>> support more than one parameter.
> 
> That's fair. I actually was referring to things like the example
> above (using it with macros!), and statements that I found that
> C-generics - to "make most sense" - would be used with macros.
> (Myself, I cannot judge.)
> 

_Generics are very often wrapped in macros.  And certainly long macros 
have the inconvenience of needing line continuation characters when 
split into multiple lines.  But that's an inconvenience of macros in 
general, rather than a _Generic thing.

> 
>>
>>> N.B.: I've been heavily using C++ polymorphism in sophisticated
>>> designs but never(?) needed RTTI. (Clean OO-programming made it
>>> unnecessary to switch on types.) I used generic data structures
>>> a lot in C++, though, and their advantages (compared to "C")
>>> are obvious. I also never needed introspection. - I guess it's
>>> depending on the programming paradigms one chooses.
>>>
>> I not only have never needed RTTI, but I have it disabled - for small 
>> systems embedded use, "-fno-rtti" and "-fno-exceptions" are very common. 
> 
> I suppose you've done that only because of the application domain
> you are or were working in.
> 

Sure.

RTTI, and to a fair extent exceptions, are considered "low cost" in C++ 
because their implementation usually involves little run-time speed 
costs as you have lots of tables of static data (like unwind 
information) instead.  In the kind of embedded systems I work with, a 
large lump of mostly unused read-only data or code is a far more 
significant cost than on a "big" system where it's just disk space that 
might not even get loaded into ram.

And in "big" system coding, you are usually targeting average throughput 
speeds - it's okay that a rarely used path is very slow when an 
exception is raised, if that means the common path can be faster.  With 
small embedded systems, it is the worst case timing that is typically 
the critical measure.  So again, different tradeoffs mean exceptions are 
usually disabled in the kinds of systems I work with.

(I also write code in Python on PC's, and use exceptions there without 
the same kinds of concerns.)

> (When we had started with C++ in our professional development we
> had neither RTTI nor exceptions available. With exceptions we'd
> not have had necessity to implement an own framework and company
> standard for that. It would certainly have been useful.)
> 
>> I think RTTI is perhaps more important if you have complicated 
>> exceptions in code - exception types can be passing through your 
>> handlers that don't really know much about the types being thrown.
> 
> (I don't see how that would be imposed by the use of exceptions.)

I did not say (or mean to imply) that the use of RTTI is /imposed/ or 
required for exceptions.  You can certainly use one without the other. 
But I believe that one use of RTTI is in more advanced exception 
handling.  (Imagine a function that catches any exception passing 
through, logs some information about it, then re-throws it.)

> 
>>
>> Once I am able to make use of C++26 and reflection in my code, I can 
>> definitely see use-cases for it.  At the very least, my debug printfs 
>> will have nicer output for enumeration types :-)
> 
> Yes, that's true; to spare you writing a bit own code to write in
> this case.
> 

Yes.  It also helps keep things consistent.  Making inconsistencies as 
close to impossible as practical is more important than reducing the 
code written.

A particularly likely use-case would be for "count the enumerators" 
situations.  Often you have :

	enum States {
		state_initialising,
		state_idle,
		state_starting,
		state_running,
		state_stopping,

		no_of_states
	}

or perhaps

	constexpr auto last_state = state_stopping;

There are a few ways to write your count of enumerators, and lots of 
enumeration uses where such a count can be useful.  Each method has its 
disadvantages, such as needing manual update when the enumeration is 
changed, or introducing "fake" values (like "no_of_states" above) that 
don't really belong in the enumeration and mess with useful compiler 
warnings like "-Wswitch".  With C++26 reflection, it will be easy to 
have things like :

	constexpr auto no_of_states = count_enum(^^States);

Technical Details
Message-ID: <11f1beh$2vtg1$1@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> <11euckd$1ht9i$1@raubtier-asyl.eternal-september.org> <11eugtg$erg$4@dont-email.me> <11euj8b$1lhf1$1@dont-email.me> <11f17h5$2dad1$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 16:43:13 +0100
Organization: A noiseless patient Spider
Lines: 129
Message-ID: <11f1beh$2vtg1$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>
 <11euckd$1ht9i$1@raubtier-asyl.eternal-september.org>
 <11eugtg$erg$4@dont-email.me> <11euj8b$1lhf1$1@dont-email.me>
 <11f17h5$2dad1$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 15:43:14 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3143169"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+Gpxtwl8Uek58Rtl6AwNMULCnT2AxFHxg="; posting-host="e6744434c7b28607c9aa71bc7804445d"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:S7JmqeSwbTAoDTbGaZaSkxkB3ww= sha256:HddL5EsKW416IdWRGKVNEAu3JTj9rGfiLtU4cgpT99s=
	sha1:E95+9YdAUeYr/QEmKkMgwv9v2SY= sha256:2UmwIgjGnSpll5aqMc0E+n9eZmclh4jubcwKImApbQM=
Content-Language: en-GB
In-Reply-To: <11f17h5$2dad1$1@dont-email.me>