C++20 introduces designated initialisers. I'm trying to initalise a character array inside a struct with the new syntax.
Pretty simple problem, I think code may explain it best:
struct Test
{
unsigned v;
char b[12];
};
int main()
{
char str[12] = "hello"; // Works.
char strb[12]{"hello"}; // Works.
//
Test testa
{
.v = 100u,
.b = "hello" //Doesn't work...
};
Test testb
{
.v = 100u,
.b = { "hello" } //Doesn't work...
};
Test testc
{
.v{ 100u },
.b{ "hello" } //Doesn't work...
};
Test testd
{
.v{ 100u },
.b{ "hello" } //Doesn't work...
};
Test teste
{
.v = 100u,
.b = {} //This works.
};
Test testf
{
.v = 100u,
.b = {'h', 'e', 'l', 'l', 'o', '\0'} //This works.
};
return 0;
}
I find this behaviour bazzar, the char str[12] = "hello"; line works just fine. But the same initaliation form doesn't work in designated initialiser lists.
Question : Why can't I initialize the char array with a string literal?
Edit
I was previously using GCC. This works with clang. Is there a workaround for GCC and, is clang or GCC correct?