Live data from Hacker News

Generics in C without void* or macros – enabled by psychec

github.com

51–60 of 74 posts

Re: Generics in C without void* or macros – enabled by psychec

#51

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...

_Generic has been created for a specific use (tgmath.h) and is not very powerful.

Re: Generics in C without void* or macros – enabled by psychec

#52

I 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

#53
post #50

Earlier 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.

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

#54
post #47

Earlier 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.

Pretty much any CUDA programmer can talk about how they got 1024 threads working on 64kB "shared memory" with their C++ compiler. That "shared memory" region is exceptionally fast, so its a good idea to hyper-optimize your memory accesses to use that region.

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

#55

Earlier 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 mainly write code for embedded devices, so whatever programs I write on x86 is to interface with those devices, hence I'm quite comfortable staying with C

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

#56
post #52

I 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

Templates are a particular implementation of generics.

Re: Generics in C without void* or macros – enabled by psychec

#57
post #50

Earlier 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.

While the "major conveniences" are orthogonal to exceptions, constructors are not, and in many non-trivial cases constructors should be expected to fail.

Re: Generics in C without void* or macros – enabled by psychec

#58
post #45

I 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...

Re: Generics in C without void* or macros – enabled by psychec

#59
post #32

Earlier 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.

I'm no compiler expert, but I would expect that compilers would put exception-handling code on a separate 64-byte 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

#60
post #58
post #45

I 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...

Yes! Typedefs are definitely worth the time spent learning them.
Post reply on HN