C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 11:07 AM   #1
Registered User
 
Join Date: Sep 2009
Posts: 5
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.
danath is offline   Reply With Quote
Old 09-12-2009, 12:11 PM   #2
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is offline   Reply With Quote
Old 09-12-2009, 08:48 PM   #3
Registered User
 
Join Date: Sep 2009
Posts: 5
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
danath is offline   Reply With Quote
Old 09-13-2009, 03:58 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Wikipedia has a huge amount of information. Perhaps it's a good starting point?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-13-2009, 04:41 AM   #5
The larch
 
Join Date: May 2006
Posts: 3,082
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).

Quote:
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.

Quote:
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).
anon is offline   Reply With Quote
Old 09-14-2009, 08:20 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Old 09-14-2009, 10:00 PM   #7
Registered User
 
Join Date: Sep 2009
Posts: 5
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.
danath is offline   Reply With Quote
Old 09-14-2009, 10:02 PM   #8
Registered User
 
Join Date: Sep 2009
Posts: 5
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?
danath is offline   Reply With Quote
Old 09-15-2009, 03:14 AM   #9
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Reply

Tags
array, c++0x, initialization

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to initialize a private static const array of a private child class in a class? ryanli C++ Programming 3 04-10-2009 02:20 AM
how to initialize char * vignesh C Programming 3 03-30-2009 01:17 AM
How do you use variable to initialize array size? yougene C Programming 11 09-04-2007 02:50 PM
initialize constant and initialization list sawer C++ Programming 5 07-09-2006 06:30 AM
using constructor to initialize data Unregistered C++ Programming 6 03-19-2002 08:55 PM


All times are GMT -6. The time now is 08:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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