Thread: Declare array then assign with a comma seperated list

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    Declare array then assign with a comma seperated list

    I know I can do this:

    Code:
    int a[4] = {1,2,3,4};
    but I want to do this:

    Code:
    int a[4];
    a = {1,2,3,4};
    What is this called and what version of gcc do I need?

    Thanks!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645
    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.
    Code:
    #include<vector>
    int main()
    {
        std::vector<int> foo;
        foo = {1,2,3,4,5};
    }
    It works for me, with gcc 4.7.2.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,170
    Though primitive arrays are not supported, unfortunately.
    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.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645
    Quote Originally Posted by whiteflags View Post
    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.
    What if you want to replace the whole set of data at once ?
    This would look and behave better than constructing another vector and swapping.
    Last edited by manasij7479; 11-14-2012 at 08:30 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,170
    How is that not what happens, anyway?

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645
    Quote Originally Posted by whiteflags View Post
    How is that not what happens, anyway?
    You have a point..

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thanks guys. Weird thing is that mingw will compile:

    Code:
    int a[4];
    a = {1,2,3,4};
    but gcc 4.7 won't. Is there a bug with mingw that is allowing me to do this?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,057
    Related thread
    http://cboard.cprogramming.com/cplus...ssignment.html


    Message I got on unofficial TDM MinGW g++ 4.7.? build

    Code:
    main.cpp|7|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]|
    After doing either -std=c++11 or -std=gnu++11 I still get this error.
    Code:
    main.cpp:7:17: error: assigning to an array from an initializer list
    Tim S.
    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

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Quote Originally Posted by stahta01 View Post
    Related thread
    http://cboard.cprogramming.com/cplus...ssignment.html


    Message I got on unofficial TDM MinGW g++ 4.7.? build

    Code:
    main.cpp|7|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]|
    After doing either -std=c++11 or -std=gnu++11 I still get this error.
    Code:
    main.cpp:7:17: error: assigning to an array from an initializer list
    Tim S.
    lol gcc > mingw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help wanted: reading a comma seperated file
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 11-17-2010, 12:54 PM
  2. comma at end of intialization list
    By matsp in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2008, 02:13 PM
  3. How do you declare a list to be global.....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 07-06-2005, 02:03 PM
  4. comma seperated lists
    By ozzy34 in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2004, 12:18 PM
Website Security Test