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

1

8
#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
    
Lowspirited answered 12/1, 2019 at 0:16 Comment(7)
I think the code is correct and it's a gcc bug (although not related to the other question you linked)Tse
Does indeed work on clang. wandbox.org/permlink/7oAmpGhjS4o7u7OaSilver
as an aside, does adding an explicit nameless temporary yields the exact same result? Like so: const A& art = A{1, "ab"}; This would sort it for what I'm doingLowspirited
I'm not sure it's even valid to count on a simpler initialization like std::array<char, 12> c{"hello"};. Although a raw array member of type T[N] is the most likely implementation, the Standard only says that a std::array<T,N> is an aggregate which can be list-initialized from an initializer list with up to N elements. So an implementation like template <class T, size_t N> struct array { T __elem; array<T, N-1> __rest; }; would also be compliant.Ensure
@Ensure in aggregate initialization it says that a string literal can be used to initialize a char array (and std::array is specified as being an aggregate containing an array)Tse
@Lowspirited that's what the code means anyway, so if the compiler accepts it then go for itTse
Oops, my recursive definition wouldn't support the requirement of being a contiguous container, or the data() member (unless maybe the implementation knows indexing the elements as array elements will work in practice). But @Tse I don't see any requirement that std::array contains an array element.Ensure
E
3

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"}
Evannia answered 12/1, 2019 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.