Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking support for _Generic() selection in C

I use macro generic selection to "overload" some functions in my custom library and i want to make it as portable as possible, so i'm trying to check whether generic selection support is present by doing

#if ((__STDC_VERSION__>=201112L) || ((__GNUC__*10000+__GNUC_MINOR__*100+__GNUC_PATCHLEVEL__)>=40600) || ((__clang_major__*10000+__clang_minor__*100+__clang_patchlevel__)>=30100) || (__xlC__>=0x1201))

(CHECK THE EDIT NOTE ON THE BOTTOM TO SEE ACCURATE COMPILER VERSIONS)

As these compiler versions should support SOME c11 features, but i'm not actually sure whether generic selection is actually supported on these versions; could anyone confirm? alternatively is there another way?

.

.

EDIT: compiler version that support _Generic keyword are actually:

((__GNUC__*10000+__GNUC_MINOR__*100+__GNUC_PATCHLEVEL__)>=40900) || ((__clang_major__*10000+__clang_minor__*100+__clang_patchlevel__)>=30000) || (__xlC__>=0x1201)
like image 820
Alex Sim Avatar asked Mar 09 '17 14:03

Alex Sim


1 Answers

The strict way to check this is

#if __STDC__==1 && __STDC_VERSION >= 201112L

A compiler may only define __STDC__ to the value 1 if it is a conforming implementation (reference: C11 6.10.8.1). Any conforming implementation with __STDC_VERSION >= 201112L must implement _Generic.

There may however be compiler versions that supported _Generic before they had full C11 support - those you have to find in some compiler-specific way.

like image 159
Lundin Avatar answered Sep 22 '22 14:09

Lundin