Live data from Hacker News

I write type-safe generic data structures in C

danielchasehooper.com

101–110 of 196 posts

Re: I write type-safe generic data structures in C

#101
post #60
post #41

Earlier quoted context omitted.

> Delegating to an external vtable (mandatory to avoid overhead) means that you have to forward-declare all of the types you'll ever use a vtable with. We went down the rabbit hole of writing a compiler for this as part of a project I used to work on (Apache Clownfish[1], a subproject of the retired Apache Lucy project). We started off parsing .h files, but eventually it made sense to create our own small header lang…

i am firmly of the opinion that compiling to c is a better route than doing clever c tricks to sort of get what you want. the compiler can be pretty minimal and as you note it pays for itself.

There’s some prior work called CFront. It implements a superset of C that’s just an “increment”. I think it’s worth looking into, it might take off one day!

Re: I write type-safe generic data structures in C

#102
> Structurally identical types will be considered the same type in GCC 15 and Clang later in 2025 thanks to a rule change

Beware that only tagged unions are considered the same type under the new rule, provided they have the same structrure and the same tag.

The List(T) macro should be changed to generate a different tag for each different T. Which is trivial (with ##) for simple one-word types, but impossible for even mildly complex ones like pointers to char (strings).

Of course you can force yourself to typedef any type before using it in a List, but it looses much of its versatility. Example:

  typedef char *str;
  List(str) my_list_of_str;
  List(str) tokenize(str input) {...}

Re: I write type-safe generic data structures in C

#103
post #34

Earlier quoted context omitted.

Do you have a couple of real world examples?

Any established C codebase, for example the kernel or Postgres? Traditionally microcontroller firmwares as well, though those are increasingly friendly to C++, you just have to be careful about allocations as C++ makes it way easier to accidentally allocate than C does.

Nothing is stopping you from linking C++ code to Postgres.

Re: I write type-safe generic data structures in C

#104

Earlier quoted context omitted.

And you can write them when you need them. The situation where you need a red black tree with 10 different key/value combos isn’t real.

You could take away anything you use and say "but we could make it ourselves", that doesn't mean it's helpful.

Except it’s very common for C programs to contain one-off data structures, so it’s not a hypothetical. It’s a concrete programming style.

Re: I write type-safe generic data structures in C

#105

Earlier quoted context omitted.

When all you have are arrays, everything looks like a problem you solve with arrays. There are quite a few problems that specialised containers are suited for, that's why they were created.

And you can write them when you need them. The situation where you need a red black tree with 10 different key/value combos isn’t real.

If, by "situation", you mean the development of a small program with so many constraints that using existing libraries is out if the question, then yes.

Otherwise, that seems unwise to me. Not every user of a generic type has to be generic. A major selling point of generic types is that you write a library once, then everyone can instantiate it. Even if that is the only instance they need in their use case, you have saved them the trouble of reinventing the wheel.

No colleague of mine may need 10 different instances of any of my generic libraries, but I bet that all of them combined do, and that our bosses are happy that we don't have to debug and maintain 10+ different implementations.

Re: I write type-safe generic data structures in C

#106

Earlier quoted context omitted.

I know it used to be, but is it really still common for embedded systems to use weird architectures that G++/Clang don't support?

Unless it is a popular system or common architecture, yes.

Could you show me an example of a micro controller still supported today which doesn't have a C++ compiler?

Re: I write type-safe generic data structures in C

#107

Earlier quoted context omitted.

I believe that since C23 foo() is now a nullary function. As this is the last approved standard and it supersedes all previous standards, it is technically correct to say that de-jure this is what the (unqualified) C standard mandates. Of course de-facto things are more nunanced.

C23 does not change anything in this situation, because we are talking about the definition of main(), not a forward declaration. More details here: https://news.ycombinator.com/item?id=38729278#38732366

In what situation fn() doesn't mean fn(void) under C23?

Re: I write type-safe generic data structures in C

#108

Earlier quoted context omitted.

You could take away anything you use and say "but we could make it ourselves", that doesn't mean it's helpful.

Except it’s very common for C programs to contain one-off data structures, so it’s not a hypothetical. It’s a concrete programming style.

Sure, but it is also very common for C programs to contain data structures that have one use in the program, and could still be instances of a generic type. You mentioned red black trees, which are a perfect example of that.

Re: I write type-safe generic data structures in C

#109
post #107

Earlier quoted context omitted.

C23 does not change anything in this situation, because we are talking about the definition of main(), not a forward declaration. More details here: https://news.ycombinator.com/item?id=38729278#38732366

In what situation fn() doesn't mean fn(void) under C23?

None, but that is not my point. Before C23, fn() already meant the same thing as fn(void) in function definitions, which the situation under discussion here.

C23 changed what fn() means outside a function definition.

Re: I write type-safe generic data structures in C

#110
post #56

Earlier quoted context omitted.

Thanks, this post is about C. On some projects you must use C.

If I may may be provocative :-) this post isn't about C. It's about layering on a custom language using C preprocessor macros. My compilers were originally written in C. I started using the C preprocessor to do metaprogramming. After some years I got fed up with it and removed nearly all of the preprocessor use, and never looked back. My code was much easier to understand. An amusing story: long ago, a friend of mine…

I could tell a similar story (many, in fact) about C++'s templates. It is not entirely clear to me what exactly makes the preprocessor a bad choice. One could argue that it is too flexible, so it is possible to create a mess with it. But somehow this seems a rather weak argument for inventing another monomorphization layer, which often evolve into their own mess.
Post reply on HN