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

后端 未结 2 1586
礼貌的吻别
礼貌的吻别 2020-12-20 11:39

I have the following function:

void doStuff(int unusedParameter, ...)
{
    va_list params;
    va_start(params, unusedParameter);
    /* ... */
    va_end(p         


        
相关标签:
2条回答
  • 2020-12-20 12:05

    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__)
    
    0 讨论(0)
  • 2020-12-20 12:14

    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.

    0 讨论(0)
提交回复
热议问题