Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Argument Lists in C++/CLI

How do I create a function which accepts a variable argument list in C++/CLI? I am looking to create a function which forwards most of it's arguments to String::Format.

like image 210
Agnel Kurian Avatar asked Dec 01 '09 09:12

Agnel Kurian


People also ask

What is VA list in C?

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.

What is variable arguments in C?

Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement. 1) Sum of given numbers.

How does Va_arg work in C?

In the C Programming Language, the va_arg function fetches an argument in a variable argument list. The va_arg function updates ap so that the next call to the va_arg function fetches the next argument. You must call the va_start function to initialize ap before using the va_arg function.


1 Answers

Declare the last argument as a managed array prefixed with an ellipsis.

Here is a variable argument function that just passes all of its arguments to String::Format

String ^FormatAString(String ^format, ...array<Object^> ^args)
{
  return String::Format(format, args);
}

And here is how to call it:

Console::WriteLine(FormatAString(L"{0} {1} {2}.", 40.5, "hello", DateTime::Now));
like image 52
Joe Daley Avatar answered Sep 22 '22 05:09

Joe Daley