I am trying to create overloaded functions using the _Generic macro in C11, and I settled on supporting functions of null arguments, for example:
#define msg(_1) _Generic((_1), char*: msg_string, default: msg_none)(_1) char* msg_none(void){ return moo_string("Have a nice day!"); } char* msg_string(char* message){ int msglen = strlen(message); char* result = malloc(msglen + 3); sprintf(result, "<%s>\n", message); return result; }
Now compile and run:
printf("%s",msg("hello!"));
goes without problems, but:
printf("%s",msg());
causes an error:
main.c:7:17: error: expected expression printf("%s",msg());
I use:
clang --version clang version 3.5.0 (tags/RELEASE_350/final) Target: x86_64-pc-linux-gnu Thread model: posix
GCC throws:
main.c:7:5: warning: implicit declaration of function '_Generic'
so I understand that _Generic is not supported by this version of gcc:
gcc --version gcc (Gentoo 4.8.3 p1.1, pie-0.5.9) 4.8.3
Is my problem even solvable, or am I just overestimating the capabilities of _Generic, or do I just need to update my compilers to use these options correctly?
c gcc macros c11 clang
mucka
source share