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.
Generic Containers in C: Vec
71–80 of 91 posts
Re: Generic Containers in C: Vec
#72Earlier quoted context omitted.
Not with C23.
Only if the macro only declares structs, and not any functions.
Re: Generic Containers in C: Vec
#73Many 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…
Re: Generic Containers in C: Vec
#74Earlier 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.
Re: Generic Containers in C: Vec
#75From 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.
Re: Generic Containers in C: Vec
#76Earlier 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)?
Re: Generic Containers in C: Vec
#77Many 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.
Re: Generic Containers in C: Vec
#78Earlier 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?
Re: Generic Containers in C: Vec
#79Re: Generic Containers in C: Vec
#80And 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...