frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

How to write type-safe generics in C

https://raphgl.github.io/blog/generics-in-c.html
24•todsacerdoti•1h ago

Comments

self_awareness•58m ago
Well, another option would be to use a C++ compiler, which supports templates, but limit the use of classes through a coding convention standard.
lelanthran•45m ago
> Well, another option would be to use a C++ compiler, which supports templates, but limit the use of classes through a coding convention standard.

When the other option is "ask the developers to practice discipline", an option that doesn't require that looks awfully attractive.

That being said, I'm not a fan of the described method either. Maybe the article could have shown a few more uses of this from the caller perspective.

krupan•40m ago
"ask the developers to practice discipline" is a baseline requirement for coding in C
pjmlp•31m ago
Unfortunately the majority has failed to attend the temple classes on such practices.
lelanthran•31m ago
> "ask the developers to practice discipline" is a baseline requirement for coding in C

Sure, but since there's 10x more opaque footguns in C+++, there is much less discipline needed than when coding in C++.

The footguns in C are basically signed-integer over/underflows and memory errors. The footguns in C++ include all the footguns in C, and then add a ton more around object construction type, object destruction types, unexpected sharing of values due to silent and unexpected assignments, etc.

Just the bit on file-scope/global-scope initialisation alone can bite even experienced developers who are adding a new nonlocally-scoped instance of a new class.

Loudergood•19m ago
If only.
krupan•42m ago
Not sure why this is down voted when the whole point of TFA is to torture the C language into doing something it can't really do. I guess there's an unspoken assumption in TFA that you are stuck using C and absolutely cannot use a different language, not even C++?
pjmlp•32m ago
C folks rather reproduce badly C++ than acknowledge its Typescript like improvements over C.
lelanthran•18m ago
> C folks rather reproduce badly C++ than acknowledge its Typescript like improvements over C

This is a rather crude misrepresentation; most C programmers who need a higher level of abstraction than C reach for Java, C# or Go over C++.

IOW, acknowledging that C++ has improvements over C still does not make the extra C++ footguns worth switching over.

When you gloss over the additional footguns, it looks like you're taking it personally when C programmers don't want to deal with those additional footguns.

After all, we don't choose languages based on which one offers the most freedom to blow your leg off, we tend to choose languages based on which ones have the most restrictions against blowing your leg off.

If your only criteria is "Where can I get the most features", then sure, C++ looks good. If your criteria is "Where are the fewest footguns", then C++ is at the bottom of the list.

pjmlp•14m ago
Nah, it is called life experience meeting those kind of persons since the 1990's, starting on BBS forums.

My criteria is being as safe as Modula-2 and Object Pascal, as bare minimum.

C++ offers the tools, whereas WG14 has made it clear they don't even bother, including turning down Dennis Ritchie proposal for fat pointers.

lelanthran•13m ago
>> looks like you're taking it personally

> it is called life experience meeting those kind of persons

Looks like you are confirming that you are taking it personally.

I don't understand why, though.

You cannot imagine a programmer that wants fewer footguns?

pjmlp•11m ago
Yes, when careless programmers are responsible for critical infrastructure systems, and rather take a YOLO attitude to systems programming.
lelanthran•3m ago
> Yes, when careless programmers are responsible for critical infrastructure systems, and rather take a YOLO attitude to systems programming.

Well, that's a novel take: "Opting for fewer footguns is careless". :-)

It's probably not news to you that your view is, to put it kindly, very rare.

flashgordon•3m ago
Actually this was my first instinct too. Just limit what you use c++ for and write c code with templates and be done with it.

The problems I am guessing start when you are tempted into using the rest of the features one by one. You have generics. Well next let's get inheritance in. Now a bit of operator overloading. Then dealing with all kinds of smart pointers...

schaefer•48m ago
A more accurate title would be:

“How to write type safe generics using the C preprocessor”

lelanthran•43m ago
Having used this in production around 2004, I never really liked this approach - abusing the preprocessor this much shouldn't be necessary and the result is almost unreadable.

I think a better way is possible using `_Generic`[1]. Even though it would still use macros, the resulting code is much more readable.

---------------------------

[1] `_Generic` comes with its own problems too, of course.

synergy20•39m ago
or use nim metaprogramming, which will be transpiled to c
david2ndaccount•38m ago
I wrote a similar article in the past: https://www.davidpriver.com/ctemplates.html

I use this technique in my hobby projects as it has worked out the best for me of the options I’ve explored.

variadix•35m ago
You can create macro functions per generic function so something like Vector_New(int)(&v) expands to Vector_New_int(&v). It also looks less foreign (more like templates) than the G macro.
pjmlp•33m ago
Type safety, generics and C is a bit of oxymoron.
jesse__•29m ago
I wrote a metaprogramming language that adds another option to the list, for anyone that's interested : https://github.com/scallyw4g/poof

The Zhee Showroom

https://zheeshowroom.com/
1•mahdihabibi•44s ago•1 comments

Ucs-detect: automatically test the Unicode version and support level of a termin

https://ucs-detect.readthedocs.io/
1•fanf2•4m ago•0 comments

Sagittarius A*, the supermassive black hole at the center of the Milky Way

https://en.wikipedia.org/wiki/Sagittarius_A*
1•kaycebasques•5m ago•0 comments

Goofing on Meta's AI Crawler

https://bruceediger.com/posts/goofing-on-meta/
1•bediger4000•7m ago•0 comments

Solving Amazon's Infinite Shelf Space Problem

https://worksonmymachine.ai/p/solving-amazons-infinite-shelf-space
1•Stwerner•8m ago•0 comments

Bernardo Kastrup on the anticipation for Google's Gemini 3 release

https://mastodon.nl/@BernardoKastrup/115554155793916448
1•netfortius•9m ago•0 comments

Show HN: A free CLI tool to extract LinkedIn data

https://github.com/linkdAPI/linkedin-leads-discover
1•LinkdAPI•12m ago•0 comments

Minimalism in Art and Design

https://minimalissimo.com
1•keiferski•12m ago•0 comments

Show HN: Rovr – a modern terminal file explorer

https://github.com/NSPC911/rovr
1•NSPG911•19m ago•0 comments

Show HN: I Built the Anti-Social Network (and Social Media Billionaires Hate It)

https://eintercon.com/
4•abilafredkb•20m ago•2 comments

Two books I recommend to developers

https://notes.eatonphil.com/books-developers-should-read.html
1•ibobev•20m ago•0 comments

Weighting an Average to Minimize Variance

https://www.johndcook.com/blog/2025/11/12/minimum-variance/
1•ibobev•21m ago•0 comments

Programming from a Smartphone

https://olekgor.substack.com/p/programming-on-a-smartphone-my-approach
1•agornostal•21m ago•0 comments

Show HN: Research on NeXT ('85–'97) + Interview with Co-Founder Dan'l Lewin

https://repository.gatech.edu/entities/publication/dfd5ec68-c3c5-4c15-9e94-81e510d5e134
2•zaxel1995•24m ago•0 comments

Kodak Ran a Secret Nuclear Device in Its Basement for Decades

https://www.popularmechanics.com/science/energy/a69147321/kodak-film-nuclear-reactor/
2•cainxinth•24m ago•1 comments

What I Learned From a Stroke at 26

https://www.nytimes.com/2016/09/25/jobs/what-i-learned-from-a-stroke-at-26-make-time-to-untangle....
2•suioir•24m ago•0 comments

Linux on the Fujitsu Lifebook U729

https://borretti.me/article/linux-on-the-fujitsu-lifebook-u729
9•ibobev•26m ago•2 comments

Archie Guardian: Local AI Security Monitoring

https://github.com/archiesgate42-glitch/archie-guardian
1•ArchieGate•28m ago•1 comments

Neural basis of the association between future time perspective and ADHD

https://www.sciencedirect.com/science/article/abs/pii/S0278584625001812
2•DrierCycle•28m ago•0 comments

Show HN: Animate Video Game Sprites with Prompts

https://gametorch.app/sprite-animator
1•gametorch•31m ago•0 comments

Mouse Accuracy

https://mouseaccuracy.com/
1•denysonique•33m ago•0 comments

Bayesian nonlinear asymptotic regression model

https://world.hey.com/apetrov/bayesian-nonlinear-asymptotic-regression-model-a1a4bdca
1•apetrov•34m ago•0 comments

The 1B Token Challenge: Finding the Perfect Pre-Training Mix

https://huggingface.co/blog/codelion/optimal-dataset-mixing
2•codelion•38m ago•0 comments

Patterns for Faster Python Code

https://blog.jetbrains.com/pycharm/2025/11/10-smart-performance-hacks-for-faster-python-code/
2•birdculture•39m ago•0 comments

Amazon Pulls Out of Mamdani's NYC–Hundreds Laid Off in Manhattan

https://www.msn.com/en-us/money/smallbusiness/amazon-pulls-out-of-mamdani-s-nyc-hundreds-laid-off...
4•delichon•39m ago•5 comments

You're a computer science major, don't panic

https://www.nytimes.com/2025/11/12/opinion/ai-coding-computer-science.html
1•azhenley•39m ago•0 comments

Immunometabolic dysregulation in depression predates illness onset

https://www.nature.com/articles/s44220-025-00538-9
1•DrierCycle•42m ago•0 comments

Show HN: Lightweight exchange rate checker (no cookies, no ads)

https://myip.casa/generalutilities/exchangerate
1•myip_casa•42m ago•0 comments

6B Miles Driven

https://www.tesla.com/fsd/safety
13•mensetmanusman•43m ago•1 comments

Docker Compose Continuous Deployment

https://github.com/kimdre/doco-cd
1•naimo1984•48m ago•0 comments