I am trying to do object-orientation in C and want to have a syntactic sugar macro for the notation
object->vtable->method(object, arg1, arg2)
into
send(object, method, arg1, arg2)
Unfortunately when a method takes no argument, the trailing comma problem arises
send(object, method)
gives
object->vtable->method(object, )
Is there any portable (no ##__VA_ARGS__
or Visual Studio) way of doing this?
I figured out one but I need to swap the object and the method
#define FIRST_ARG_(N, ...) N
#define FIRST_ARG(args) FIRST_ARG_(args)
#define send(msg, ...) \
FIRST_ARG(__VA_ARGS__)->vtable->msg(__VA_ARGS__)
permits
send(method, object)
send(method, object, arg1, arg2)
Edit
With the help of two good answers from below I will do it with these macros. It works up to 16 arguments but can easily be extended
#define SEND_NO_ARG(obj, msg) obj->vtable->msg(obj)
#define SEND_ARG(obj, msg, ...) obj->vtable->msg(obj, __VA_ARGS__)
#define GET_18TH_ARG(arg1, arg2, arg3, arg4, arg5, \
arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, \
arg16, arg17, arg18, ...) arg18
#define SEND_MACRO_CHOOSER(...) \
GET_18TH_ARG(__VA_ARGS__, \
SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, \
SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, \
SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, SEND_ARG, \
SEND_NO_ARG, )
#define SEND(...) SEND_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
#define send(method, object, ...) object->vtable->method(object,##__VA_ARGS__)
is not portable ?