Declare array then assign with a comma seperated list

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]; ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @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 !



  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 07:30 PM.
    Manasij Mukherjee | gcc-4.8.2 @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 !



  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    How is that not what happens, anyway?

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by whiteflags View Post
    How is that not what happens, anyway?
    You have a point..
    Manasij Mukherjee | gcc-4.8.2 @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 !



  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
    2,551
    Related thread
    Brace-enclosed initializer list in an assignment


    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 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

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Quote Originally Posted by stahta01 View Post
    Related thread
    Brace-enclosed initializer list in an assignment


    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, 11:54 AM
  2. comma at end of intialization list
    By matsp in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2008, 01: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, 01:03 PM
  4. comma seperated lists
    By ozzy34 in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2004, 11:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21