在 C++ 中使用数组调用具有可变数量参数的函数; (就像Python的*运算符)
我正在尝试用 C++ 编写一个 v8 模块;在那里,函数接收数组中可变数量的参数。我想获取该数组并调用像 gettext
和 printf
这样的函数来接收格式化的字符串和必需的参数。问题是,如何获取一个数组并将元素作为参数发送给这些函数之一?
在 python 中,我会做这样的事情:
def the_function(s, who, hmany): print s%(who, hmany)
the_args = ["Hello, %s from the %d of us", "world", 3]
the_function(*the_args)
如何在 C++ 中完成? (我使用的是 v8 和 node.js,所以这些命名空间中可能有一个我不知道的函数或类)
I'm trying to write a v8 module in C++; there, the functions receive a variable number of arguments in an array. I want to take that array and call a function like gettext
and printf
that receives a formatted string and it's necessary args. The thing is, how can one take an array and send the elements as arguments to one of those functions?
In python, I'd do something like this:
def the_function(s, who, hmany): print s%(who, hmany)
the_args = ["Hello, %s from the %d of us", "world", 3]
the_function(*the_args)
How can that be accomplished in C++? (I'm using v8 and node.js, so maybe there's a function or class somewhere in those namespaces that I'm not aware of)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种方法:
假设您正在尝试执行 printf。基本上,va_list 是关键,您可以使用它来检查参数,或将它们传递给采用 va_list 的其他函数。
Here's one way:
Assuming that you're trying to do a printf. Basically, va_list is the key, and you can use it to either examine the arguments, or pass them to other functions that take va_list.