![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 5
| 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 = ****
}
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 | |
| | #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:
| |
| anon is offline | |
| | #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
};
Thanks Last edited by danath; 09-13-2009 at 07:03 AM. Reason: typo |
| danath is offline | |
| | #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:
| |
| Elysia is offline | |
| | #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:
__________________ I might be wrong. Quote:
| ||
| anon is offline | |
| | #6 |
| Cat without Hat 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});
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 | |
| | #7 | |
| Registered User Join Date: Sep 2009
Posts: 5
| Quote:
| |
| danath is offline | |
| | #8 | |
| Registered User Join Date: Sep 2009
Posts: 5
| Quote:
Do you know which compiler is supporting this? | |
| danath is offline | |
| | #9 |
| Cat without Hat 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 | |
![]() |
| Tags |
| array, c++0x, initialization |
| Thread Tools | |
| Display Modes | |
|
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 |