c++ - What is the reason for not being able to deduce array size from initializer-string in member variable? -
consider code:
struct foo { const char str[] = "test"; }; int main() { foo foo; } it fails compile both g++ , clang++, spitting out essentially
error: array bound cannot deduced in-class initializer
i understand standard says, there particular reason why? since have string literal seems compiler should able deduce size without problem, case when declare out-of-class const c-like null terminated string.
the reason have possibility override in class initializer list in constructor. guess in end confusing.
struct foo { foo() {} // str = "test\0"; // implementing easier if can see how big `str` is, foo() : str({'a','b', 'c', 'd'}) {} // str = "abcd0" const char str[] = "test"; }; notice replacing const char static constexpr works perfectly, , want anyway