可变参数函数 - 如何确保参数正确传递
有没有什么方法(内置或代码模式)来确保向可变参数函数传递正确数量的参数? (显然,这将作为 API 的一部分包含在内,我可以检查自己的内部代码。)
我正在考虑要求 UN32 Magic Number 作为最后一个传递的参数,并检查可变参数函数中的有效性。有人对此有什么想法吗?
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.)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 PP_NARG 宏来添加半自动计数。
gcc -E 产生:
You could use the PP_NARG macro to add a count semi-automatically.
gcc -E produces:
va_*
宏只是从本地堆栈中弹出变量,因此您必须信任用户。我认为传递数组/大小元组可能更安全。va_*
macros just pop the variables from the local stack, so you have to trust the user. passing an array/size tuple could be safer, I think.C 或 C++ 中没有明确的方法来确保将正确数量的参数传递给可变参数函数。即使需要签名也不能保证有效,因为它可能与有效参数的值发生冲突。传递一个向量<>可能会更好。因为检索到的元素计数是准确的。
There is no definitive way in C or C++ to ensure that the correct number of arguments have been passed to a variadic function. Even requiring a signature is not guaranteed to work as it may clash with the value of a valid argument. You will probably be much better off passing a vector<> as the element count retrieved is accurate.
不能将 C++0x 的可变参数模板功能应用于函数吗?这将生成一个类型安全的 vararg 函数。
请参阅此链接,其中使用可变参数模板函数实现类型安全的 printf
http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates
Couldn't you use the variadic template feature of C++0x applied to a function? This would generate a vararg function that is type-safe .
See this link with it's type-safe printf implementation using a variadic templated function
http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates
这取决于您所说的“确保可变参数函数传递正确数量的参数”的含义...
传递 UN32 幻数作为最后一个参数将允许您确定参数列表的结束位置,即它们的总数。因此,通过计算在 UN32 之前找到的参数数量,您就知道自己有多少个参数,并且您的函数应该知道它是否足够。不知道您是否可以在运行时确定这一点(可能为时已晚)...
无论如何,通常可变参数函数都有一个固定的参数列表部分,代表强制参数(至少一个);所以可能这应该是您确保函数获得正确数量的参数的方法......
It depends on what you mean by "ensure that a variadic function is passed the correct number of parameters"...
Passing a UN32 Magic Number as last argument will allow you to determine where the list of arguments ends, so their overall number. So, by counting how many arguments you have found before UN32, you know how many arguments you have and your function should know whether is it enough. Don't know if it is ok for you to determine this at run-time (it could be too late)...
Anyway, usually variadic functions have a fixed argument list portion representing the mandatory arguments (at least one); so possibly this should be the way for you to ensure that the function gets the correct number of arguments...
不,不可能。
可变参数以无法修复的方式破坏类型安全。
如果您想要恢复类型安全,请考虑将可变参数函数分解为多个类型安全
[小]类的成员函数,用于保存调用之间的共享状态。
这总是可能的,即使与单个变量相比,多个调用可能看起来很尴尬
称呼。
共享状态可能是您首先想要可变参数函数的原因。
以 iostream 与 printf 为例。
No. Not possible.
Variadic breaks type-safety in a way that cannot be fixed.
If you want type-safety back, then consider breaking variadic function into several typesafe
member function of a [small] class that holds the shared state between their calls.
This is always possible, even if multiple calls might look awkward compared to single variadic
call.
Shared state is probably why you wanted variadic function in the first place.
Take iostream vs printf as example.