tags:

views:

640

answers:

3

That is the question.

+6  A: 

For unmanaged C++ with the same convenient syntax, no.

But there is support for variable argument lists to functions in 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.

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.

LBushkin
+6  A: 

Yes. In standard C++, you can use va_arg and the ... syntax. See MSDN for details.

For C++/CLI, There is a shortcut for this.

You do this as:

void TheMethod( String^ firstArgument, ... array<Object^>^ variableArgs );

See this blog post for details.

Reed Copsey
Nice to know about that ... wasn't aware C++ had this extension for managed code.
LBushkin
@LBushkin: Updated to the better syntax.
Reed Copsey
A: 

There is a named parameters library in boost (if I understood correctly what params in C# are). It allows writing functions like this:

int y = lib::f(_name = "bob", _index = 2);

Can't tell anything about if there is a significant overhead involved.

Eugene
C# params isn't named params - it's variable length argument lists.
Reed Copsey