0

Suppose class foo which contains as member a statically allocated array.

class foo
{
 private:
  char d_chars[5];

 public:
  foo();
};

Is there any compiler flag that can produce a warning if the initializer list has a different size rather than the size of the array?

For example, the following it may be syntactically valid, but perhaps the developer forgot to add all the necessary initializers.

foo::foo() :
  d_chars{'A', 'B', 'C', 'D'}

On the other hand, I know that if the initializer list exceeds the size of the array, the compiler gracefully will produce an error.

T.C.
  • 133,968
  • 17
  • 288
  • 421
Manos
  • 2,136
  • 17
  • 28
  • This is not exactly what you're looking for, but something similar is [-Wmissing-field-initializers](https://gcc.gnu.org/onlinedocs/gcc-4.9.3/gcc/Warning-Options.html#index-Wmissing-field-initializers-503) (which is included with `-Wextra`). But this detects missing uninitialized fields (e.g. of a struct) rather a statically allocated array. If your `d_chars` was a struct of five chars (they'd have to be named) instead of an array and your initializer list was `: d_chars({'A'})` it would complain that there aren't enough values. – e0k Jan 22 '16 at 17:38
  • Hmm I see. Thanks. However, I need it for `int` arrays containing a large set of initial values. – Manos Jan 22 '16 at 17:43
  • This question is similar to [this question](http://stackoverflow.com/questions/3659839/can-i-cause-a-compile-error-on-too-few-initializers). They basically say that if the initialization list is too short, the remaining values get default initialized. (You probably knew that part.) Their conclusion is that you can't generate such a warning, but it's an old post (5yr) and so they're also quoting older documentation. Maybe they've added something since then. It would, of course, also depend on the compiler (you're using the `g++` tag, so I assume gcc) and compiler version you're using. – e0k Jan 22 '16 at 18:00
  • Yeah I already know it. But I hoped if there was any warning, especially for newer gcc versions (5.2+). – Manos Jan 22 '16 at 23:01

0 Answers0