Is it possible to implement the std :: array-like container (thin shell around the C built-in array) with C ++ 0x initializer_list?
Yes , well, while you are ready to cheat. As Mooing Duck points out, no, it doesn't even fool you if compiler developers don't let you . Although, you can still get close enough - you can use initializer lists and a static array that is hidden by the shell.
This is the code I wrote for my personal toolkit. The key is to ignore the size altogether, even for the array, and let the provider container handle it; in this case, initializer_list , which can provide the size via std::distance , thereby avoiding explaining the size of the client side (the term that I just came up with seems to be).
Since this is "someone could have come up with this", this is not a code, but problems with its "return" to the public; in fact, I got the idea from some expert guy whose nickname I don’t remember on the Freenode ##c++ channel, so I think this is a recognition for them:
* EDIT * ed:
template <typename T> struct carray {
Using and declaring in C ++ 0x mode is pretty simple (just tested with GCC 4.6 in Fedora 15), but it works with the caveats mentioned in the external links above, so this behavior seems to be undefined:
using lpp::carray; carray<short const> CS = {1, -7, 4, 188};
However, I do not understand why the compiler developer in any case will not implement the initializer_list of integrals as a static array. Your call.
Not only does it work this way, if you can #ifdef the initializer constructor in the path in pre-C ++ 0x mode, you can use it in pre-C ++ 0x; although a preliminary formulation of the data array as its own variable is required, this is IMHO, closest to the original intention (and has the advantage that it can be used and does not cause, for example: problems with the area). (also tested using the above compiler, as well as the Debian Wheezy GCC):
using lpp::carray; short data[]= {1, -7, 4, 188}; carray<short const> CS (data);
There! There is no "size" parameter anywhere!
We would also like to provide a constructor to indicate the size if a list of initializers was not provided.
Sorry, this is one of the features that I have not mastered. The problem is how to allocate memory “statically” from an external source, possibly for Allocator. Assuming that this can be done somehow through the allocate helper functor, then the constructor would be something like this:
explicit carray (size_t N) : ax(allocate(N)), sx(N) {}
I hope this code helps, as I can see that the question is more or less old.