Type 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...
Generics in C without void* or macros – enabled by psychec
51–60 of 74 posts
Re: Generics in C without void* or macros – enabled by psychec
#52I 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
#53Earlier quoted context omitted.
> Most of the conveniences C++ offers to the programmer are not possible without exceptions Like what?
Pretty much anything involving constructors that fail.
Re: Generics in C without void* or macros – enabled by psychec
#54Earlier quoted context omitted.
One of the reasons C++ is so complicated is that you don't pay for the features that you don't use. If you wanted generics in C, you can easily use C++ without any of those other features (baggage, if you will) and not pay for it. Heck, you could define a few macros and make it impossible to use any baggage features you don't like. I've used C++ in a pretty tight embedded environment and it was great.
> I've used C++ in a pretty tight embedded environment and it was great. I'd love to hear some details. I'm always looking for C++ stories that didn't end in disaster and what the people did that worked.
There are some very interesting constraints in GPU-programming. Fitting C++ classes and even some data-structures in the "shared memory" region is pretty nifty.
Re: Generics in C without void* or macros – enabled by psychec
#55Earlier quoted context omitted.
Yes, that was confusing. "Look, you don't need to use these C-features if you use another language than C."
C++ 14, Haskell and Python. One of the reasons I'm actually going for pure C in this modern day and age, is that I can just write a small program, without having any other dependency. I'm sure the same could be said for C++, too, but for some reason, with my setup (GCC 5.1.0 with MinGW on Windows 7), I'm averaging around 800KB on C++ vs 80KB on C, for the (mostly) same code. EDIT: It appears that exception-handling i…
I'm in the same situation and I use C# for windows based tools.
Re: Generics in C without void* or macros – enabled by psychec
#56I 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…
Generics != templates
Re: Generics in C without void* or macros – enabled by psychec
#57Earlier quoted context omitted.
Pretty much anything involving constructors that fail.
It's easy to not write constructors that can fail. The major conveniences like templates, RAII, overloading, algorithms, lambdas, virtual functions, etc. work fine without exceptions.
Re: Generics in C without void* or macros – enabled by psychec
#58I like C for the small set of basic rules and that it does not impose much cognitive overhead of learning too many new things for a given project I have to get to know. Typedefs were already too much and I prefer not to use them...
Re: Generics in C without void* or macros – enabled by psychec
#59Earlier quoted context omitted.
Exceptions don't generally slow down execution -- they usually have zero runtime cost if they're not thrown making them better than error returns for performance. However, the exchange is, as you said, that they do add a lot of overhead to the file-size.
Something that generates extra code that ends up in the instruction cache during execution is not zero cost. There's cost to transfer it through memory and cache hierarchies and there's cost when it causes the CPU to drop some other L1C cache line.
You only use up "cache" if you actually hit the cache. If you're on a different L1 cache line (and never execute), you'll never be in L1 instruction cache, maybe never even in L2 or even L3 cache lines.
Not that I'm an expert on compilers... but whenever I look at assembly, there are lots of "NOPs" across the code. I've always assumed it was for some kind of boundary alignment, probably cache.
Re: Generics in C without void* or macros – enabled by psychec
#60I like C for the small set of basic rules and that it does not impose much cognitive overhead of learning too many new things for a given project I have to get to know. Typedefs were already too much and I prefer not to use them...
Typedefs are nice if you do not want to write 'struct' all the time...