What's a good way to check availability of __restrict keyword?
I am looking a set of #ifdef
's to check availability of __restrict
keyword for GCC and Visual Studio. I assume that it needs to check compiler version, but I don't know for which versions it was introduced. Any开发者_开发技巧one that can help me out?
UPDATE: This must (and only needs to) work when compiling as C89! So I cannot rely on __STDC_VERSION__
indicating C99, or C99 support.
In a 'configure, make, make install' scenario, this should be checked in 'configure'. 'configure' should define a 'HAS_RESTRICT' in config.h. This should in turn be checked in your headers to define a suitable macro.
For visual studio, I have zero idea.. :(
Just use the C99 standard keyword restrict
, and possibly #define
it to something else.
You can test for C99 conformance with, for example:
#if __STDC__ != 1
# error not conforming
# define restrict __restrict /* use implementation __ format */
#else
# ifndef __STDC_VERSION__
# error not conforming
# define restrict __restrict /* use implementation __ format */
# else
# if __STDC_VERSION__ < 199901L
# error Compiler for C before C99
# define restrict __restrict /* use implementation __ format */
# else
# /* all ok */
# endif
# endif
#endif
int fx(int *restrict a, char *restrict b) {
*b = *a;
return 0;
}
int main(void) {
int a[1];
char b[1];
fx(a, b);
return 0;
}
Of course the #error
s should be edited out in a working version
How I fixed it:
#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
# define SKP_restrict __restrict
#elif defined(_MSC_VER) && _MSC_VER >= 1400
# define SKP_restrict __restrict
#else
# define SKP_restrict
#endif
IMHO, __restrict
should be available in all standard compilers for both C/C++ programs. It's similar to C99 restrict
in certain way.
精彩评论