Are there gotchas using varargs with reference parameters

前端 未结 6 1954
臣服心动
臣服心动 2020-11-29 08:11

I have this piece of code (summarized)...

AnsiString working(AnsiString format,...)
{
    va_list argptr;
    AnsiString buff;

    va_start(argptr, format);         


        
6条回答
  •  悲&欢浪女
    2020-11-29 08:43

    Here's what the C++ standard (18.7 - Other runtime support) says about va_start() (emphasis mine) :

    The restrictions that ISO C places on the second parameter to the va_start() macro in header are different in this International Standard. The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

    As others have mentioned, using varargs in C++ is dangerous if you use it with non-straight-C items (and possibly even in other ways).

    That said - I still use printf() all the time...

提交回复
热议问题