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

后端 未结 2 1599
礼貌的吻别
礼貌的吻别 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: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.

提交回复
热议问题