Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this work? (brace-initialization of references)

#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
    
like image 442
haelix Avatar asked Jan 12 '19 00:01

haelix


1 Answers

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"}
like image 68
Jans Avatar answered Nov 11 '22 19:11

Jans