Live data from Hacker News

Workarounds for C11 _Generic()

chiark.greenend.org.uk

11–20 of 58 posts

Re: Workarounds for C11 _Generic()

#11
post #8

Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?

Templates are so intertwined with the C++ type system such that bringing "just templates" to C would require also bringing in a bunch of C++ features if you want templates to behave like they do in C++. Just for starters, you would need name mangling and/or function overloading.

Re: Workarounds for C11 _Generic()

#12
At the bottom of the page lemonade is made:

> Apart from the most obvious answer (that it’s useful for things exactly like ), the best thing I’ve thought of to do with _Generic is to use it for deliberate error checking.

> The annoyance in all the previous sections was that it was very hard to avoid compile errors, when we wanted our code to actually compile, run and do something useful. But if what we wanted was to provoke compile errors on a hair-trigger basis, then perhaps we could use it for that more reliably?

Re: Workarounds for C11 _Generic()

#13
post #8

Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?

If C wanted a form of generics, it could likely do much better than templates. Concepts might've improved things, but I'm stuck on C++17 right now so I can't speak with experience.

Re: Workarounds for C11 _Generic()

#14

The only reason I can imagine for this behaviour is that the compiler/standard writers did not want it. Maybe C just shouldn't include generics. Especially not as part of the macro layer.

The macros in tgmath and more recently stdbit show why those could be necessary.

If you have a set of functions for addition with overflow detection, say add_overflow{i,l,ll}, and you have a pair of ptrdiff_t’s or int32_t’s or whatnot that you know are standard integer types, and you want to use the appropriate add_overflow* function, can you do it?

With _Generic you can. Without it I think you’re stuck providing separate functions for every integer typedef in the standard library and then requiring all library authors to do the same for both integer typedefs and integer-accepting functions that they define.

(_Generic is not part of the macro level, that’s why the semantic-checking issues discussed in the article even arise. The C preprocessor can still be implemented as a separate binary that doesn’t understand C itself, even in C23.)

Re: Workarounds for C11 _Generic()

#16
post #8

Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?

Just use C++ with just templates?

That would be my advice too. What was the point of “You don’t pay for what you don’t use” if nobody’s going to use it?

Re: Workarounds for C11 _Generic()

#18

The only reason I can imagine for this behaviour is that the compiler/standard writers did not want it. Maybe C just shouldn't include generics. Especially not as part of the macro layer.

As a member of the WG14 I can tell you that _Generic does cause a fair bit of issues and complications in the language, and as a user of C i think _Generic is bad, because it mostly useful for confusing users about what code does so, Yes C would be better off without _Generic. Please don't use it.

Re: Workarounds for C11 _Generic()

#19
post #4
post #2

According to this the "big bug" is that... _Generic works mostly like a macro and expands code that the compiler sees. That seems like a little weak, macros have been doing this forever via a mere extra level of indirection. So sure, "(x)->length" might not be valid syntax in all configurations the compiler might see. But "LENGTH(x)" is, e.g.: #if X_MIGHT_BE_MYSTRINGBUFFER #define LENGTH(x) ((x)->length) #else #defin…

Macros and #ifdef do not solve the problems that _Generic is used for. _Generic is for static polymorphism, not for target or build configuration.

Macros are perfectly capable of static polymorphism. That is one of their primary use cases. _Generic is simply an alternative mechanism for static polymorphism, with some extra capabilities and some limitations in comparison to your typical macro.
Post reply on HN