Alex Rivera | Logout

Is there any way to pass an anonymous array as an argument in C++?

Asked 2009-07-16T22:16:22.693
34

I'd like to be able to declare an array as a function argument in C++, as shown in the example code below (which doesn't compile). Is there any way to do this (other than declaring the array separately beforehand)?

#include <stdio.h>

static void PrintArray(int arrayLen, const int * array)
{
   for (int i=0; i<arrayLen; i++) printf("%i -> %i\n", i, array[i]);
}

int main(int, char **)
{
   PrintArray(5, {5,6,7,8,9} );  // doesn't compile
   return 0;
}
Edit
Report

1 Answer

4

Yes and no. In the current version of the standard (ISO C++ 1998 with ammendments from 2003), it is not possible. However, in the next version of the standard "C++0x" (which, despite its name implying that it will be released in 200x, will most likely be released in 2010), it will be possible with std::initializer_list<>.

answered 2009-07-16T22:22:32.730

Your Answer