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.
I write type-safe generic data structures in C
101–110 of 196 posts
Re: I write type-safe generic data structures in C
#102Beware 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
#103Earlier 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.
Re: I write type-safe generic data structures in C
#104Earlier 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.
Re: I write type-safe generic data structures in C
#105Earlier 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.
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
#106Earlier 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.
Re: I write type-safe generic data structures in C
#107Earlier 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
Re: I write type-safe generic data structures in C
#108Earlier 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.
Re: I write type-safe generic data structures in C
#109Earlier 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?
C23 changed what fn() means outside a function definition.
Re: I write type-safe generic data structures in C
#110Earlier 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…