c++gccc++17

Error in aggregate initialization in assignment for a struct with an array data member


This following piece of code fails with gcc 8.5 up to 10.5 with g++ -std=c++17 (https://godbolt.org/z/od99f93Tx):

struct T { char arr[2]; };

int main()
{
    T data;
    data = { "V" };
    return 0;
}

The error is:

source>: In function 'int main()':
<source>:6:18: error: no match for 'operator=' (operand types are 'T' and '<brace-enclosed initializer list>')
     data = { "V" };
                  ^
<source>:1:8: note: candidate: constexpr T& T::operator=(const T&)
 struct T { char arr[2]; };
        ^
<source>:1:8: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const T&'
<source>:1:8: note: candidate: constexpr T& T::operator=(T&&)
<source>:1:8: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'T&&'
Compiler returned: 1

But it works in gcc 11.1 with the same -std=c++17 flag. If you change the array member to some other non-array type (like int or whatever), and replace "V" with some compatible value of course, it works in all versions from 8.5 to 11. Was it a gcc bug that was fixed in gcc 11, or rather some standard defect related to aggregate initialization that wasn't solved until later? Or what?


Solution

  • This is a GCC bug, filed as member char array with string literal initializer causes = {} to fail.

    It seems to have originated from changes in C++14.

    It was Fixed for GCC 11.