#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:
array<char>
.Something that works is exploding the string literal into characters:
const A& ar = {1, {'a', 'b'}}; // works
The first thing to notice is that the initializer {1, "t"}
is using brace elision to initialize the sub-aggregate A.c
which means that the literal "t"
is taken to directly initialize the internal array that std::array
holds. In this case, this array would look like char data[12]
.
This reduces to say that we are initializing a reference to const A
with a brace-list containing an element that initializes a member array.
This will be somewhat equivalent to:
struct S {
char const data[2];
};
S const& s = {"t"}; // fail for gcc
GCC has already a bug report on that.
You've already provided a workaround in the comment section. Just initialize the reference as:
const A& ar = A{1, "t"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With