Sunday, January 15, 2012

C11 - Generic Selections

C99 introduced type-generic macros defined in the new tgmath.h header. These are macros that expand to the appropriate function based on the type of the provided argument. For example, their are 6 different arc cosine functions: 3 for the different real floating point types and 3 for the different complex floating point types. If you #include <tgmath.h>, a macro with the name acos is exposed which will expand to a call to one of the 6 functions based on the argument type.

The type-generic mechanism employed to make this work was not exposed or made available to the programmer but was instead left up to implementation magic. C11 introduces the generic selection expression which provides a mechanism to make compile-time choices based on type allowing type-generic macros to be created using standard C constructs.

In this post I will introduce and explore some of the applications of the new generic selection.

Tuesday, January 10, 2012

C11 - Static Assertions

C11 provides the new _Static_assert declaration which allows the use of compile-time assertions. This post describes this new feature and the practical benefits.