Variable length argument list of set type

arguments, c, c++, function

Solution

A g++ specific solution:

const size_t MAX_ARGS = 42;

void Function(int argc, const unsigned short (&args)[MAX_ARGS])
{
}

int main()
{
    Function(5, {1,2,3,4,5});
}

If you don't actually need the `argc` parameter, you can write a template:

template<size_t N>
void Function(const unsigned short(&args)[N])
{
    // use N
}

This works in C++03. The array needs to be const, or else you can't pass a temporary initializer list like that. If you need to modify the elements inside the function, you'll need to make a copy.

Problem

Alright, I'm pretty sure that this has been discussed before in some way or another, but I'm apparently too stupid to find it. First: I'm NOT looking for va_list and the other macros. What I am looking for is something like the main-function parameters. The default prototype, as all of you know is: ``` int main(int argc, char *argv[]); ``` Now, I want something similar for my program, but don't know how exactly. Let's assume we have this function: ``` void Function(int argc, unsigned short *args[]) { for(int i = 0; i < argc; ++i) printf("%hu ", args[i]); } ``` And I want something like this function call: ``` Function(5, 1, 2, 3, 4, 5); ``` Would that work? Because I don't want the 'cluttering' of va_list, nor do I want to create: ``` void AnotherFunction() { unsigned short Args[] = { 1, 2, 3, 4, 5 }; Function(5, Args); } ``` Simply because in that case I would only need a simple pointer. Could someone please point me in the right direction? Thank you very much. Edit: Thank you everyone for your valuable input. I'll settle for 'Doesn't work with standard C/C++ for now and look for a different approach to my problem. Again, thank you very much.

Original source