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
.
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.
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With