How do I use std::initializer_list to initialize a struct?












-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?










share|improve this question























  • Why do you need a constructor?

    – Justin
    Nov 13 '18 at 18:10
















-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?










share|improve this question























  • Why do you need a constructor?

    – Justin
    Nov 13 '18 at 18:10














-1












-1








-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?










share|improve this question














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?







c++ c++11 constructor clang






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:03









RipreadRipread

84




84













  • Why do you need a constructor?

    – Justin
    Nov 13 '18 at 18:10



















  • Why do you need a constructor?

    – Justin
    Nov 13 '18 at 18:10

















Why do you need a constructor?

– Justin
Nov 13 '18 at 18:10





Why do you need a constructor?

– Justin
Nov 13 '18 at 18:10












1 Answer
1






active

oldest

votes


















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).






share|improve this answer



















  • 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











  • @Justin Unlikely to work before that :P

    – Lightness Races in Orbit
    Nov 13 '18 at 18:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287013%2fhow-do-i-use-stdinitializer-list-to-initialize-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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).






share|improve this answer



















  • 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











  • @Justin Unlikely to work before that :P

    – Lightness Races in Orbit
    Nov 13 '18 at 18:29
















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).






share|improve this answer



















  • 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











  • @Justin Unlikely to work before that :P

    – Lightness Races in Orbit
    Nov 13 '18 at 18:29














0












0








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).






share|improve this answer













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).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 18:07









Lightness Races in OrbitLightness Races in Orbit

288k51470798




288k51470798








  • 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











  • @Justin Unlikely to work before that :P

    – Lightness Races in Orbit
    Nov 13 '18 at 18:29














  • 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











  • @Justin Unlikely to work before that :P

    – Lightness Races in Orbit
    Nov 13 '18 at 18:29








1




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





"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













@Justin Unlikely to work before that :P

– Lightness Races in Orbit
Nov 13 '18 at 18:29





@Justin Unlikely to work before that :P

– Lightness Races in Orbit
Nov 13 '18 at 18:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287013%2fhow-do-i-use-stdinitializer-list-to-initialize-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values