Live data from Hacker News

Generic Containers in C: Vec

uecker.codeberg.page

71–80 of 91 posts

Re: Generic Containers in C: Vec

#71

An important property of C++ templates is that their instantiations are de-duplicated at link-time. If you have two dependencies in C which both use the same generic container macros, and both instantiate vec(int) you'll fail-out with redefinition errors.

Not with C23.

Only if the macro only declares structs, and not any functions.

Re: Generic Containers in C: Vec

#72

Earlier quoted context omitted.

Not with C23.

Only if the macro only declares structs, and not any functions.

Sorry, I don't understand what macro you're referring to. Your post addressed "vec(int)", and suggested it will fail if repeated, which was true before C23 but not anymore. What functions are defined through vec(int)?

Re: Generic Containers in C: Vec

#73
post #22

Many C programmers need proper generic programming mechanisms (perhaps something like Zig's comptime) in C, but macros are the worst possible approach, and they don't want to switch to a different language like C++. As a result, they struggle with these issues. This is what I think the standardization committee should focus on, but instead, they introduced _Generic.

The most insulting thing about _Generic is the name. Really? _Generic? For a type-based switch with horrific syntax? What were they thinking... That said, generic programming in C isn't that bad, just very annoying. To me the best approach is to write the code for a concrete type (like Vec_int), make sure everything is working, and then do the following: A macro Vec(T) sets up the struct. It can then be wrapped in a…

The name has to be ugly, new names in C are always taken from the set of reserved identifiers: those starting with an underscore & a capital letter, or with two underscores. Since they didn't reserve any "normal" names, all new keywords will be stuff like `_Keyword` or `__keyword`, unless they break backwards compatibility. And they really hate breaking backwards compatibility, so that's quite unlikely.

Re: Generic Containers in C: Vec

#74
post #69
post #58

Earlier quoted context omitted.

If the templates are monomorphized, each instantiation of a templated function will have a different address. To acquire the address of any given instantiation we need a symbol in the object file.

What isn't clear to me why one would ever want monomorphization in the first place.

exactly !

Re: Generic Containers in C: Vec

#75
post #68
post #61

From my experience, trying to make C type safe is counter productive. It's perfectly possible to do generic vectors in C without twisting the language. This implementation isn't as safe as alternatives in other languages, but plays well on C's strengths. https://github.com/codr7/hacktical-c/tree/main/vector

Why would you think this? My implementation is type and bounds safe and nice to use.

Because that level of (type) safety and C is a bad fit.

Re: Generic Containers in C: Vec

#76

Earlier quoted context omitted.

Only if the macro only declares structs, and not any functions.

Sorry, I don't understand what macro you're referring to. Your post addressed "vec(int)", and suggested it will fail if repeated, which was true before C23 but not anymore. What functions are defined through vec(int)?

You're right.

Re: Generic Containers in C: Vec

#77
post #22

Many C programmers need proper generic programming mechanisms (perhaps something like Zig's comptime) in C, but macros are the worst possible approach, and they don't want to switch to a different language like C++. As a result, they struggle with these issues. This is what I think the standardization committee should focus on, but instead, they introduced _Generic.

Macros are the best possibly approach, compared to C++ templates or _Generic

Re: Generic Containers in C: Vec

#78

Earlier quoted context omitted.

Your problem is not with RAII, but with reference counting, which you correctly identified should be the last resort, not the default; at least for the applications typically written in C++.

Why should reference counting be a last resort?

Because it's insanely slow, and doesn't support cyclic structures.

Re: Generic Containers in C: Vec

#79
post #75
post #68

Earlier quoted context omitted.

Why would you think this? My implementation is type and bounds safe and nice to use.

Because that level of (type) safety and C is a bad fit.

I assume you do not want to elaborate why you think it is a "bad fit". IMHO it is perfect.

Re: Generic Containers in C: Vec

#80
post #9
post #5

And if I want a vec(int *)? These token pasting 'generic' macros never work for non-trivial types.

Correct, complex types must be typedef'd. At least, until c2y integrates _Record as per N3332: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20_Record...

I am not terribly excited about this proposal. It is overly complex.
Post reply on HN