What is the reason for not being able to deduce array size from initializer-string in member variable?

前端 未结 3 769
时光取名叫无心
时光取名叫无心 2020-12-03 17:19

Consider the code:

struct Foo
{
    const char str[] = \"test\";
};

int main()
{
    Foo foo;
}

It fails to compile with both g++ and clan

3条回答
  •  温柔的废话
    2020-12-03 17:40

    If the compiler was allowed to support what you described, and the size of str was deduced to 5,

    Foo foo = {{"This is not a test"}};
    

    will lead to undefined behavior.

提交回复
热议问题