可变函数 - 如何确保参数正确传递 [英] variadic function - how to ensure parameters passed correctly

查看:194
本文介绍了可变函数 - 如何确保参数正确传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有任何方式(内置或代码模式),以确保变量函数传递正确数量的参数? (这将作为API的一部分被显示,我可以检查我自己的内部代码。)

Is there any way (built-in or a code pattern) to ensure that a variadic function is passed the correct number of parameters? (This will be included as part of an API obviously, I can check my own internal code.)

我考虑要求一个UN32魔术数字是最后一个参数传递并检查可变函数中的有效性。

I was considering requiring a UN32 Magic Number to be the last argument passed and check that for validity in the variadic function. Does anyone have any thoughts on that?

推荐答案

您可以使用 PP_NARG 宏以半自动添加计数。

You could use the PP_NARG macro to add a count semi-automatically.

int myfunc (int count, ...);
#define MYFUNC(...) myfunc(PP_NARG(__VA_ARGS__), __VA_ARGS__)

MYFUNC(a,b,c,d);
MYFUNC(a,b,c,d,e,f,g);

gcc -E产生:

int myfunc (int count, ...);

myfunc(4, a,b,c,d);
myfunc(7, a,b,c,d,e,f,g);

这篇关于可变函数 - 如何确保参数正确传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆