Is it possible to have a variadic function in C with no non-variadic parameter?

时光怂恿深爱的人放手 提交于 2019-11-29 09:14:15

Your choice is either leave it as it is and use va_list, alias it (if it's GCC) as others pointed out, or do something along the lines of exec(2) interface - passing an array of pointers requiring a NULL terminator:

/* \param args  NULL-terminated array of
 *              pointers to arguments.
 */
void doStuff( void* args[] );

Either way it would be much better to refactor the interface to somehow take advantage of the type system - maybe overload on exact argument types used:

void doStuff( int );
void doStuff( const std::string& );
void doStuff( const MyFancyAppClass& );

Hope this helps.

In GCC, you have a workaround: You can define a macro with a variable number of arguments and then add the dummy parameter in the expansion:

#define doStuff(...) realDoStuff(0, __VA_ARGS__)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!