有没有像 c++ 中的 c# params 之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1155661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anything like the c# params in c++?
提问by devoured elysium
That is the question.
就是那个问题。
采纳答案by LBushkin
For unmanaged C++ with the same convenient syntax, no.
对于具有相同方便语法的非托管 C++,没有。
But there is support for variable argument liststo functions in C++.
但是在 C++ 中支持函数的变量参数列表。
Basically you declare a function with the last parameter being an ellipsis (...), and within the body of the function use the va_start()/va_arg() calls to parse out the supplied parameter list.
基本上,您声明一个函数,最后一个参数是省略号 (...),并在函数体内使用 va_start()/va_arg() 调用来解析提供的参数列表。
This mechanism is not type safe, and the caller could pass anything, so you should clearly document the public interface of the function and what you expect to be passed in.
这种机制不是类型安全的,调用者可以传递任何东西,所以你应该清楚地记录函数的公共接口以及你期望传入的内容。
For managed C++ code, see Reed's comments.
对于托管 C++ 代码,请参阅 Reed 的评论。
回答by Reed Copsey
Yes. In standard C++, you can use va_arg and the ... syntax. See MSDN for details.
是的。在标准 C++ 中,您可以使用 va_arg 和 ... 语法。有关详细信息,请参阅MSDN。
For C++/CLI, There is a shortcut for this.
对于 C++/CLI,有一个快捷方式。
You do this as:
你这样做:
void TheMethod( String^ firstArgument, ... array<Object^>^ variableArgs );
See this blog post for details.
回答by Eugene
There is a named parameterslibrary in boost (if I understood correctly what params in C# are). It allows writing functions like this:
boost中有一个命名参数库(如果我正确理解了C#中的参数是什么)。它允许编写如下函数:
int y = lib::f(_name = "bob", _index = 2);
Can't tell anything about if there is a significant overhead involved.
无法说明是否涉及重大开销。
回答by KABoissonneault
Nowadays, with modern C++, you can use modern type-safe practices for variadic functions.
如今,使用现代 C++,您可以对可变参数函数使用现代类型安全实践。
Use either variadic templates or std::initializer_list if all your arguments have the same type
如果所有参数都具有相同类型,则使用可变参数模板或 std::initializer_list
With variadic template, you use recursion to go through a variadic parameter list. Variadic template example:
使用可变参数模板,您可以使用递归遍历可变参数列表。可变模板示例:
template<class T>
void MyFoo(T arg)
{
DoSomething(arg);
}
template<class T, class... R>
void MyFoo(T arg, R... rest)
{
DoSomething(arg);
// If "rest" only has one argument, it will call the above function
// Otherwise, it will call this function again, with the first argument
// from "rest" becoming "arg"
MyFoo(rest...);
}
int main()
{
MyFoo(2, 5.f, 'a');
}
This guarantees that if DoSomething, or any other code you run before the recursive call to MyFoo, has an overload for the type of each argument you pass to the function MyFoo, that exact overload will get called.
这保证了如果 DoSomething 或您在递归调用 MyFoo 之前运行的任何其他代码对传递给函数 MyFoo 的每个参数的类型具有重载,则将调用该确切的重载。
With std::initializer_list, you use a simple foreach loop to go through the arguments
使用 std::initializer_list,您可以使用一个简单的 foreach 循环来遍历参数
template<class T>
void MyFoo(std::initializer_list<T> args)
{
for(auto&& arg : args)
{
DoSomething(arg);
}
}
int main()
{
MyFoo({2, 4, 5, 8, 1, 0}); // All the arguments have to have the same type
}