-1

So I've got a struct

struct live_set_item
{
    uint32_t data_0;
    uint64_t data_1;
    uint64_t data_2;

    live_set_item(uint32_t data_0_, uint32_t data_1_, uint64_t data_2_)
        : data_0(data_0_), data_1(data_1_), data_2(data_2_) 
    {}
};

and I want to initialize with an initializer list like this

void foo()
{
    struct live_set_item obj;
    get_new_lsi(&obj);

    if (failed_to_initialize(obj)) {
        obj = {
            .data_0 = 2000,
            .data_1 = 2001,
            .data_2 = 2002
        };
    }
}

But I keep getting this error from Clang

test.cpp:339:21: error: no viable overloaded '='
                obj = {
                ~~~ ^ ~

test.cpp:175:16: note: candidate function (the implicit copy assignment operator) not 
viable: cannot convert initializer list argument to 'const live_set_item'
        struct live_set_item
               ^

So I found this question and tried that out

void operator=(const std::initializer_list<live_set_item>&) {}

But now it's telling me that the move assignment operator isn't valid? And my first argument is void?

test.cpp:175:16: note: candidate function (the implicit move assignment operator) not 
viable: cannot convert initializer list argument to 'live_set_item'

test.cpp:204:22: note: candidate 
function not viable: cannot convert argument of incomplete type 'void' to 
'live_set_item' for 1st argument

                void operator=(const std::initializer_list<au_live_set_item_v1>&) {}
                     ^

This is where I'm lost, I've tried changing the assignment operator a couple different ways looking at the cpp docs for std::initializer list. Nothing is sticking. Could someone tell me what I'm doing wrong?

Ripread
  • 330
  • 2
  • 10

1 Answers1

0

That's not an initializer_list you're trying to use there; it's a GCC designated initializer and you won't get it working in standard C++ no matter how hard you try.

Also, you're attempting an assignment, not an initialization (though copy assignment might be sufficient here if you get rid of the .data_0 = stuff).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    "you won't get it working in standard C++ no matter how hard you try" Designated initializers are coming in C++20. – Justin Nov 13 '18 at 18:10