With clang on macos however it seems to not work anymore.
On macos clang the concatenation seems broken (example macro_broken.cpp):
#define CONCAT(p1,p2) p1##p2
int CONCAT(hello,world) = 2;
cpp --version wschrep@walter
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
cpp macro_broken.cpp
this outputs:
1 "macro_broken.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 384 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "macro_broken.cpp" 2
int hello##world = 2;
instead of what it should be:
int helloworld = 2;
So a##b does not concatenate here. Similarly the handy #a -> "a" also does not work.
A workaround is to use more params to our x macros in order to have different versions (but definately not ideal)
#define PLUGIN_METHODS \
METHOD(plugin_execute_t, plugin_execute, "plugin_execute", void, void) \
METHOD(plugin_add_t, plugin_add, "plugin_add", bool, int, int) \
METHOD(plugin_sub_t, plugin_sub, "plugin_sub", int, int, int)
// generate typedef statements
// example:
// typedef void (*plugin_execute_t)(void);
#define METHOD(name_t, name, name_str, ret, ...) typedef ret (*name_t)(__VA_ARGS__);
PLUGIN_METHODS
#undef METHOD
// generate function pointers
// example:
// plugin_add_t plug_add = NULL;
#define METHOD(name_t, name, name_str, ret, ...) name_t name = NULL;
PLUGIN_METHODS
#undef METHOD