Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?
Workarounds for C11 _Generic()
11–20 of 58 posts
Re: Workarounds for C11 _Generic()
#12> 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()
#13Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?
Re: Workarounds for C11 _Generic()
#14The 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.
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()
#15Not directly related but does some c/c++ compiler implement a combination of flags that create a sort "c with templates" version of C ?
Re: Workarounds for C11 _Generic()
#16Not 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?
Re: Workarounds for C11 _Generic()
#17Re: Workarounds for C11 _Generic()
#18The 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.
Re: Workarounds for C11 _Generic()
#19According 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.