开发者

variable argument list in windows va_list

开发者 https://www.devze.com 2022-12-30 00:18 出处:网络
I wanted to have function which will accept as foo(...) { //...... } u开发者_运维百科sage of this would be

I wanted to have function which will accept as

foo(...)
{
    //......
}

u开发者_运维百科sage of this would be

foo(1,2,3);
foo(1)
foo(1,2,3,4,5,5,6);

va_list can be used but again for that I have to pass foo(int count, ...), as this at run time i dont know how many argument i have.

any pointer would be appreciated

Thanks


You need to provide the function some way to determine how many arguments it's called with: either a count (or something from which a count can be determined, such as printf's format argument), or a terminator (none of the arguments will be allowed to be equal to the terminator, except the last one that must be exactly equal to the terminator). The function can't just know "by magic" when its variadic list of arguments is finished.


Variadic functions in C and C++ are not type-safe. So you cannot do what you want without some mechanism for marking the end of the argument list. You also need to have at least one "fixed" argument so that the macros can know where the start of your argument list is in memory.

This is the reason why passing the "count" as the first argument is useful: it solves both of those problems in one hit.

Note that it works out OK for things like printf because the format string is a fixed parameter, and the "count" of variadic argument is implicit in the format string.


It's not possible to build an argument list at runtime in C, so it is absolutely certain that you know how many arguments each call to f() is using at compile-time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号