I know I can do this:
but I want to do this:Code:int a[4] = {1,2,3,4};
What is this called and what version of gcc do I need?Code:int a[4]; a = {1,2,3,4};
Thanks!
I know I can do this:
but I want to do this:Code:int a[4] = {1,2,3,4};
What is this called and what version of gcc do I need?Code:int a[4]; a = {1,2,3,4};
Thanks!
It is called a brace-enclosed-extended-initializer-list ! (seriously)
Though primitive arrays are not supported, unfortunately. But most STL containers are.
Here is an example.
It works for me, with gcc 4.7.2.Code:#include<vector> int main() { std::vector<int> foo; foo = {1,2,3,4,5}; }
Then again, it seems rather odd that you would completely construct foo and then assign the values of foo to it, with an initializer list. That's not how initialization is supposed to work. So, you could argue that the language always has supported this for arrays.Though primitive arrays are not supported, unfortunately.
Thanks guys. Weird thing is that mingw will compile:
but gcc 4.7 won't. Is there a bug with mingw that is allowing me to do this?Code:int a[4]; a = {1,2,3,4};
Related thread
Brace-enclosed initializer list in an assignment
Message I got on unofficial TDM MinGW g++ 4.7.? build
After doing either -std=c++11 or -std=gnu++11 I still get this error.Code:main.cpp|7|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]|
Tim S.Code:main.cpp:7:17: error: assigning to an array from an initializer list
Last edited by stahta01; 11-15-2012 at 01:06 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson