2

Is there a way to pass an array to a function using its initial curly brace values?

For Example:

void doSomething(?)
{
    array[5] = ?;
}

int main()
{
    doSomething({1,2,3,4,5});
    return 0;
}

By the way, I don't think I can include initializer_list with VS 2012, I don't believe it exists. Supposedly I hear it's useful for these cases.

2 Answers2

0

You cannot include initializer_list in VS 2012, because it's not available in that version. Microsoft added it with VS2013. See http://msdn.microsoft.com/en-us/library/vstudio/hh567368(v=vs.120).aspx

Martin
  • 911
  • 5
  • 7
0

List initialization is only available since C++11. Passing a braced init list as a function argument tries to list initialize the corresponding argument. Thus, any type which can be list initialized would be a valid argument type for your case. In particular, aggregate types, initializer_list-constructible types and types with a constructor matching the types in the braced init list(non-narrowing conversions only) would work.

Here are a few possible signatures for doSomething:

void doSomething(initializer_list<int>);
void doSomething(vector<int>);

struct Agg
{
  int a,b,c,d,e;
};
void doSomething(Agg);

struct Constructible
{
Constructible(int, int, int, int, int);
};
void doSomething(Constructible);
Pradhan
  • 16,391
  • 3
  • 44
  • 59