[cfe-dev] cfe-dev Digest, Vol 60, Issue 109

Steve Ramsey clang at lucena.com
Tue Jun 19 11:07:27 PDT 2012


On Jun 19, 2012, at 9:49 AM, Chandler Carruth <chandlerc at google.com> wrote:
> On Tue, Jun 19, 2012 at 1:20 AM, Jay Foad <jay.foad at gmail.com> wrote:
>> I'd really like to be able to get a warning when "almost all" elements
>> of an array are initialised:
>> 
>> #define N 8
>> const int a[N] = { 3, 1, 4, 1, 5, 9, 2 };
>> warning: some but not all array elements initialised
> 
> At least in C++, this warning wouldn't make a lot of sense because all of
> the array elements *are* initialized. [dcl.init.aggr] 8.5.1/7: "If there
> are fewer initializers in the list than there are members in the aggregate,
> then each member not explicitly initialized shall be value-initialized
> (8.5)."
> 
> We would have a problem with lots of code which intended to initialized the
> first few elements, and zero out the rest.

Exactly, which makes this sort of a self-inflicted wound. So, why bother specifying N in the array declaration? It’s not like you’re using std::array. One idea is to come at the problem a different way:

#define NEW_ARRAY(TYPE_, NAME_, SIZE_, ...)	\
   TYPE_ NAME_[] = {__VA_ARGS__};	\
   static_assert ((sizeof (NAME_) / sizeof (TYPE_)) == SIZE_, "")

Now just go through and replace your array declarations once, and you’ll never have this problem again.

			Steve





More information about the cfe-dev mailing list