Originally Posted by
cpjust
Well I would agree that designated initializers are useful in C, but they are less useful in C++ since we have constructors in C++. Although it certainly wouldn't hurt to re-absorb all the new features from the latest version of C into the latest C++ standard.
I don't agree - there are circumstances when you want to have a constant table. If LOTS of the entries are zero [take for example a table for your own version of "isdigit, where you have to cope with 256 character values, but only 10 have a value other than zero - this is obviously not a very realistic scenario, because there is no reason not to use isdigit - but it makes for an easy example that most of those who can follow this thread will understand without further explanation].
Code:
char isDigitTable[256] =
{
['0'] = 1,
['1'] = 1,
['2'] = 1,
['3'] = 1,
['4'] = 1,
['5'] = 1,
['6'] = 1,
['7'] = 1,
['8'] = 1,
['9'] = 1,
};
Now that means that you don't have to write a comment next to each of the characters.
The compiler will do the same as a long lost of zeros [48 of them, to be precise, asssuming we are using ASCII-based character set [1] ] with 10 ones on the end [we don't need to fill in the next 190-odd characters with zero, since that is the default for initialized arrays anyways].
[1] And if we are compiling on a non-ASCII based character set, then the compiler will support the correct digits in whatever character set is being used [assuming the compiler itself understands the character set, that is].
--
Mats