4 ms·
Actually, I wish C had support for templates. It doesn't seem like templates go against C's core philosophy, and not having to cast void*'s back and forth would
by 10098 13y ago
Actually, I wish C had support for templates. It doesn't seem like templates go against C's core philosophy, and not having to cast void*'s back and forth would be nice.
- w3pm 13y agoFor a while I thought C + templates would be a great idea as well. In theory templates are a much more powerful and expressive way of generating code than macros/xmacros. However after playing around with the idea in a few C++ modules I was writing I came to the conclusion that it's just not worth it. Part of what makes C so enjoyable to write is its relatively "weak" typing and the flexibility that comes with that, primarily freely casting between pointer types including void*'s where necessary. Templates fight against that at every opportunity and I found myself spending more time trying to figure out how to wrangle my code into the template type system than actually making progress on the problem at hand. Newer additions like C++11 constexprs take that type-system frustration to an entirely new level.
- masklinn 13y ago> I wish C had support for templates Generics. Polymorphic functions and collections don't require the complexity of C++ templates.
- beagle3 13y agoC sort-of-does have a template facility through macros. Some things about it are awkward - e.g., if you need to generate identifiers (which is traditionally done with __LINE__, which is not safe) - but it can mostly give you what templates do, if you know what you are doing.
- asveikau 13y agoYou don't have to cast void * back and forth to anything. void *p = /* ... */; char *q = p; Despite what the C++ weenies will say this is valid C! Please do not commit this cardinal sin: char *p = (char*)malloc(n); No no no! No need to cast that! Please just write: char *p = malloc(n); And if somebody tells you that won't compile without an explicit cast, please kindly remind them what language you are writing. [Actually I agree with you that C++ templates are really nice, chiefly for the reason that they allow you to avoid function pointers. In C++ you can pass your "callback" to a template and it gets inlined right with the body of the function.]
- peteri 13y agoVery true. It's my biggest annoyance with Visual Studio when I write straight C code that the intellisense error checker moans about this. The compiler is of course perfectly happy when it's running in C mode. But then I'm still upset about <stdbool.h> it should get off my damn lawn.