4

In modern C++, member variables can be explicitly initialized. However, while we can omit the capacity of an array when defining a local array in a function with an initializer list, we cannot omit the capacity of an array member even if it has an initializer list.

For example, in the following code:

class A{
    int a[]{1,2,3,4};   // error
    int b[4]{};
    int c[4]{1,2,3,4};
};

This gives an error on the member variable a:

array bound cannot be deduced from an in-class initializer

I'm curious, why is this kind of definition not allowed?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sangjin Kim
  • 991
  • 1
  • 6
  • 8
  • This is a dupe https://stackoverflow.com/questions/29593207/what-is-the-reason-for-not-being-able-to-deduce-array-size-from-initializer-stri with some possible explanations given. – cigien Jun 24 '20 at 00:43

0 Answers0