Well, first off a C array IS a container...it's just a really
piss 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.)
Your code would change from:
static const char * ar_pszColors[] = {"red", "white", "blue"};
static const int numColors = sizeof(ar_pszColors) / sizeof
(ar_pszColors[0]);
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.