how to initialize std::array with std::initializer_list in C++0x?

This is a discussion on how to initialize std::array with std::initializer_list in C++0x? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am studying C++0x with gcc 4.4.0 these days. Here I need to initialize a std::array object within a ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Question how to initialize std::array with std::initializer_list in C++0x?

    Hi,

    I am studying C++0x with gcc 4.4.0 these days.

    Here I need to initialize a std::array object within a function which has a parameter of type std::initializer_list, as below:

    Code:
    template<typename T, int S>
    void Foo(std::initializer_list<T> init_list)
    {
        std::array<T, S> my_array = ****
    }
    What should I do in the place with '****' to make my_array initialized?

    T is a type which needs explicit construction, which means I can not construct my_array as default, then copy every element from init_list to it.

    The type & size of init_list can be known during compiling time, so I believe there should be some way to make use of them.

    Can anyone give me some advice?

    Thanks in advance.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I haven't been able to figure that one out. initializer_list doesn't have anything but begin and end methods, but array doesn't have a constructor that can do anything with them.

    Perhaps pass an array<T, S> instead of the initialization list:

    Code:
    template<typename T, unsigned S>
    void Foo(std::array<T, S> my_array)
    {
        ///...
    }
    
    Foo(std::array<int, 3u>{{1, 2, 3}});
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Thanks, anon.

    Your suggestion should be workable, but when the compiler sees an ini list ({...}), it will store it into an object of std::initializer_list, so to change this behavior the client programmer needs to explicitly construct std::array at the place the calling happens -- this will obviously make the code ugly.

    I may have to stick to what I did before, boost preprocessor...

    BTW, I have another question, some articles mention the new way to init built-in array which is a member:

    Code:
    class C
    {
            int n[5];
        public: 
            C(): n{0,1,2,3,4} {}//C++09 only
    };
    But seems g++4.4.0 does not support it. Where can I find more info about the new feature, the proposal?

    Thanks
    Last edited by danath; 09-13-2009 at 07:03 AM. Reason: typo

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Wikipedia has a huge amount of information. Perhaps it's a good starting point?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yet another thing you could do, perhaps, is not to use an array but just use the data in the initializer list (I think that's read-only only, though).

    BTW, I have another question, some articles mention the new way to init built-in array which is a member
    This appears to work with GCC 4.4.1. It seems that currently all compiler versions implement their own subset of the standard and some things are changing (e.g 4.4.1 lets lvalues bind to rvalue references).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In C++0x, this should work:
    Code:
    template <size_t N, typename T>
    void fn(std::initializer_list<T> init)
    {
      std::array<T, N> ar(init); // or ar{init}
    }
    
    fn<3>({1, 2, 3});
    If GCC doesn't support this, it simply means that either the initializer list support isn't there yet, or the implementation of std::array isn't.

    Similarly for the array initialization example. It should work in C++0x, but GCC isn't a complete implementation of C++0x yet.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Quote Originally Posted by anon View Post
    Yet another thing you could do, perhaps, is not to use an array but just use the data in the initializer list (I think that's read-only only, though).



    This appears to work with GCC 4.4.1. It seems that currently all compiler versions implement their own subset of the standard and some things are changing (e.g 4.4.1 lets lvalues bind to rvalue references).
    yes, I just built gcc 4.4.1 (quite smooth to build it with gcc 4.4.0, but toooo long time) and it worked to compile above syntax, good news to me.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Quote Originally Posted by CornedBee View Post
    In C++0x, this should work:
    Code:
    template <size_t N, typename T>
    void fn(std::initializer_list<T> init)
    {
      std::array<T, N> ar(init); // or ar{init}
    }
    
    fn<3>({1, 2, 3});
    If GCC doesn't support this, it simply means that either the initializer list support isn't there yet, or the implementation of std::array isn't.

    Similarly for the array initialization example. It should work in C++0x, but GCC isn't a complete implementation of C++0x yet.
    Thanks. Another good news.

    Do you know which compiler is supporting this?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. If GCC 4.4.1 doesn't, I suspect that none does so far. I know VC++ 2010 doesn't support initializer lists, and Comeau online doesn't provide the <initializer_list> header. CodeGear C++Builder 2010 doesn't support it according to its feature matrix, and I know Clang doesn't support it, so it doesn't look good.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  2. how to initialize char *
    By vignesh in forum C Programming
    Replies: 3
    Last Post: 03-30-2009, 01:17 AM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. initialize constant and initialization list
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2006, 06:30 AM
  5. using constructor to initialize data
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2002, 07:55 PM

Tags for this Thread


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