On Aug 18, 7:00 pm, Noah Roberts <roberts.n...@gmail.com> wrote:
> Jonathan Lee wrote:
> > On Aug 18, 3:30 am, James Kanze <james.ka...@gmail.com> wrote:
> >> On Aug 17, 5:34 pm, Jonathan Lee <cho...@shaw.ca> wrote:
> >>> Of course, many would say to use std::vector and std::string,
> >>> but that's a whole other conversation.
> >> Not really. This is a case where C style arrays are definitely
> >> preferable to std::vector, for several reasons.
> > Not that I would agree with using std::vector... but I thought
> > the situation of "I'm updating the size of the array manually"
> > was screaming for someone to swoop in and recommend a container.
> Well, first off a C array IS a container...it's just a really
> **** poor one for dynamic memory and is all but obsoleted by
> boost::array.
Yes and no. Boost::array (which will be std::array in the next
version of the standard) does cover a lot of the remaining uses.
But one of the nice things about C style arrays is that the
compiler can calculate the size based on the initialization
list. (If I understand correctly, in the next version of the
standard, it will be possible for the initialization list to
determine the size of an std::vector, but not the size of an
std::array. An std::vector, however, has dynamic
initialization, which means that instances with static lifetime
are still subject to order of initialization issues.)
> > I'm a little disappointed that no one has
> boost::array
> Your code would change from:
> static const char * ar_pszColors[] = {"red", "white", "blue"};
> static const int numColors = sizeof(ar_pszColors) / sizeof
> (ar_pszColors[0]);
> to:
> static boost::array<char const*, 3> ar_pszColors[] = {...};
And there's that magic number 3 in there, that has to be updated
if you change the number of elements. For 3 or 4, probably not
a big issue, but I have a number of cases where there are 20 or
30, and counting them is somewhat error prone.
> "numColors" is then simply a part of your array:
> int numColors = ar_pszColors.size();
> The one thing I don't like, but don't think there's a way
> around, is having to provide the size in the declaration. The
> good thing, what makes that less problematic, is you'll get
> notified by the compiler if you ever add an initializer and
> forget to increment the size.
But you still have to count once, and the compiler won't say
anything if you remove one (or mis-count too high).
The next release of the standard will support something like:
std::vector< std::string > colors{ "red", "white", "blue" } ;
but that implies dynamic initialization (and thus, possible
order of initialization issues), and if I understand correctly,
this notation won't work for std::array (because you have to
mention the size as part of the type).
So there is still a use for C style arrays. My rule would be no
C style arrays with other than static lifetime, and none in a
public interface.
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34