Velocity Reviews

Velocity Reviews (
-   C Programming (
-   -   Re: Thoughts on the _Generic feature? (

Jens Gustedt 10-06-2012 09:03 AM

Re: Thoughts on the _Generic feature?
Am 06.10.2012 01:25, schrieb Aaron W. Hsu:
> With C11 comes the _Generic feature to enable a form of generic
> programming. That's pretty nice, actually, from my standpoint, but I
> wonder if others who do more programming in C than I have thoughts on
> it? I use C mostly as a means to implement other languages, so I have
> some fondness for the ability to do this sort of generic programming.

I started using it on different levels and I find it quite
usefull. But it also has several drawbacks.

I use it in P99 to build up other parts of the C11 emulation, in
particular the atomic macro/function interfaces. It works quite well,
I think, but the complexity that the _Generic feature imposes in that
case is nothing that you would want to do in every day's coding: the
fact that all branches of a _Generic expression must be valid C under
all circumstances is a real head ache.

I also use it in some other project to do some sorts of function
overloading. Here it is a real help to reduce the interface complexity
for users of the project: the project has two struct types that have
similar functionality and so _Generic can be used to unify the
interface. I also use it to distinguish pointer and array arguments to
the functions.

The implementation of these interfaces is still tough, but less
sophisticated than for P99.

That said, there is also a serious flaw that is uncovered by type
generic expression, the underspecification of qualified versions of
rvalues. Before C11 the type of an rvalue has not been observable, so
the qualifiers of that type where not important. With _Generic it
becoms observable, and has quite nasty consequences.

If you want to write type generic code you'd sometimes have to
enumerate over all 8 (ordinary types) or 16 (pointer types)
combinations of qualifiers for each base type that you are interested
in. If you write e.g an interface that captures all integral types
you'd have 12 x 8 = 96 cases to consider.

Not only that such a thing is a nightmare to maintain, for the actual
compilers it also is a performance burden during compilation.

If have written ideas about that problem up on

But to come back to _Generic: it is a usefull feature, but certainly
nothing that you want to propose to beginners. But for library
implementors it can be a nice tool to wrap up interfaces.


All times are GMT. The time now is 04:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.