I want to use typeof in clang but not __typeof__
. If I use typeof without declaring it I get warning the following warning
vector.c:14:5: warning: extension used [-Wlanguage-extension-token]
vec_init(&a,5);
^
./vector.h:27:26: note: expanded from macro 'vec_init'
((vec_ptr)->data) = (typeof((vec_ptr)->data)) malloc((capacity) * (sizeof((*((vec_ptr)->data))))) \
^
1 warning generated.
then I thought defining it might solve it so I added the following code:
#ifndef typeof
#ifdef __clang__
#define typeof(x) __typeof__(x)
#endif
#endif
With the added pre-processor definition I get the following error:
In file included from vector.c:1:
./vector.h:5:9: warning: keyword is hidden by macro definition [-Wkeyword-macro]
#define typeof(x) __typeof__(x)
^
1 warning generated.
but I get no error if I use __typeof__
instead.
How to solve this warning issue on clang?
At the point of posting this answer, typeof
is not a standard feature but an experimental language extension. As such, if you insist on using it, do so with the awareness that you aren't writing standard C - and get warned accordingly from strict compilers.
However, typeof
used to be a GNU extension but it will be included in C23. Although compiler support for adding this to strict standard C is still shaky (neither clang 15 nor gcc 12.2 will allow it when compiling as -std=c2x
).
Either way, typeof
is not a preprocessor #define
but a keyword, so you can't use it with #ifndef
.
For now, you can compile in clang or gcc with -std=gnu2x
, until compiler support for C23 settles. But you still can't compile with -pedantic
or you'll get a warning for using extensions.