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!
This is a discussion on Declare array then assign with a comma seperated list within the C++ Programming forums, part of the General Programming Boards category; I know I can do this: Code: int a[4] = {1,2,3,4}; but I want to do this: Code: int a[4]; ...
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}; }
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
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.
Originally Posted by phantomotap
Last edited by manasij7479; 11-14-2012 at 07:30 PM.
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
How is that not what happens, anyway?
Originally Posted by phantomotap
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 12:06 PM.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook