#include <array>
int main()
{
struct A
{
unsigned char l;
std::array<char, 12> c;
};
const A a = {1, "t"}; // OK
const A& ar = {1, "t"}; // error: invalid initialization of reference of type 'const main()::A&' from expression of type '<brace-enclosed initializer list>'
}
(gcc 8.2, -std=c++17)
This question talks about a GCC bug, but it's old (7 years ago).
Note that I don't care about lifetime extension, I'm actually passing the temporary directly into a function for use rather than store it but I tried making the example clean.
Edit:
- I cannot make the example smaller. In particular, it has to do with the
array<char>
. - Adding more braces around "t" and still fails.
Something that works is exploding the string literal into characters:
const A& ar = {1, {'a', 'b'}}; // works