Generics in C without void* or macros – enabled by psychec
11–20 of 74 posts
Re: Generics in C without void* or macros – enabled by psychec
#12No void* or macros, but requires a special compiler frontend.
Re: Generics in C without void* or macros – enabled by psychec
#13Re: Generics in C without void* or macros – enabled by psychec
#14Re: Generics in C without void* or macros – enabled by psychec
#15Type generic expressions [1] already exist in C11. You can do this without a special compiler front-end if you need it, you would just need to define each variant by hand. These are supported in GCC and Clang with the -std=c11 flag. [1]: http://www.robertgamble.net/2012/01/c11-generic-selections.h...
Re: Generics in C without void* or macros – enabled by psychec
#16You should never typedef a pointer to an object. That’s common knowledge amongst experienced C developers and an immediate red flag, even for myself normally welcoming of anything built with Haskell.
I'm not an expert C developer but can see some semantic benefits of typedefing a pointer. Is this considered harmful because it hides the fact that you're dealing with a pointer in the first place?
If you apply qualifiers like const to your type, results might me unexpected.
It might have semantic benefits, but C developers should be trained to ignore all these strange *-symbols anyway.
Re: Generics in C without void* or macros – enabled by psychec
#17I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…
Re: Generics in C without void* or macros – enabled by psychec
#18I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…
Looking at their example:
_Generic void prepend(_Forall(node_t)** head,
_Forall(value_t) value)
{
node_t* n = malloc(sizeof(node_t));
n->value = value;
n->next = *head;
*head = n;
}
For each use of `prepend()`, their frontend defines an appropriate struct with fields `value` then `next` (in that order) whose types are inferred from the assignments in the body of the function.Re: Generics in C without void* or macros – enabled by psychec
#19I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…
Though TBH i wouldn't use OP's preprocessor either as i think that void* and macros are perfectly fine.
Re: Generics in C without void* or macros – enabled by psychec
#20I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…
The problem with C++ is that in addition to generic functionality, it also brings a ton of other baggage that even if you do not use, you still pay for (e.g. personally i really dislike how slow C++ compilers are). Though TBH i wouldn't use OP's preprocessor either as i think that void* and macros are perfectly fine.