C++ how to use emplace_back for user defined structure
I am trying to make use of emplace_back for my user defined structure:
#include <cstdint>
#include <vector>
#include <string>
struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};
struct Student
{
std::vector<Def> ent;
};
int main()
{
Student a;
a.ent.emplace_back({ {2000} });
}
I get compilation issues:
error: no matching function for call to 'std::vector<EntryDef>::emplace_back'
c++ c++11
add a comment |
I am trying to make use of emplace_back for my user defined structure:
#include <cstdint>
#include <vector>
#include <string>
struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};
struct Student
{
std::vector<Def> ent;
};
int main()
{
Student a;
a.ent.emplace_back({ {2000} });
}
I get compilation issues:
error: no matching function for call to 'std::vector<EntryDef>::emplace_back'
c++ c++11
5
Your first argument to your constructor isstd::initializer_list<int>
yet you pass{ {2}, 1 }
?
– CoryKramer
Nov 14 '18 at 14:38
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
4
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Usestd::initializer_list<int>{ 2, 1 }
.
– Piotr Skotnicki
Nov 14 '18 at 14:42
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@PiotrSkotnicki yeah, they kind of messed upinitializer_list
– bolov
Nov 14 '18 at 15:00
add a comment |
I am trying to make use of emplace_back for my user defined structure:
#include <cstdint>
#include <vector>
#include <string>
struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};
struct Student
{
std::vector<Def> ent;
};
int main()
{
Student a;
a.ent.emplace_back({ {2000} });
}
I get compilation issues:
error: no matching function for call to 'std::vector<EntryDef>::emplace_back'
c++ c++11
I am trying to make use of emplace_back for my user defined structure:
#include <cstdint>
#include <vector>
#include <string>
struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};
struct Student
{
std::vector<Def> ent;
};
int main()
{
Student a;
a.ent.emplace_back({ {2000} });
}
I get compilation issues:
error: no matching function for call to 'std::vector<EntryDef>::emplace_back'
c++ c++11
c++ c++11
edited Nov 15 '18 at 0:48
Programmer
asked Nov 14 '18 at 14:35
ProgrammerProgrammer
2,9541851103
2,9541851103
5
Your first argument to your constructor isstd::initializer_list<int>
yet you pass{ {2}, 1 }
?
– CoryKramer
Nov 14 '18 at 14:38
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
4
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Usestd::initializer_list<int>{ 2, 1 }
.
– Piotr Skotnicki
Nov 14 '18 at 14:42
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@PiotrSkotnicki yeah, they kind of messed upinitializer_list
– bolov
Nov 14 '18 at 15:00
add a comment |
5
Your first argument to your constructor isstd::initializer_list<int>
yet you pass{ {2}, 1 }
?
– CoryKramer
Nov 14 '18 at 14:38
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
4
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Usestd::initializer_list<int>{ 2, 1 }
.
– Piotr Skotnicki
Nov 14 '18 at 14:42
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@PiotrSkotnicki yeah, they kind of messed upinitializer_list
– bolov
Nov 14 '18 at 15:00
5
5
Your first argument to your constructor is
std::initializer_list<int>
yet you pass { {2}, 1 }
?– CoryKramer
Nov 14 '18 at 14:38
Your first argument to your constructor is
std::initializer_list<int>
yet you pass { {2}, 1 }
?– CoryKramer
Nov 14 '18 at 14:38
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
4
4
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }
.– Piotr Skotnicki
Nov 14 '18 at 14:42
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }
.– Piotr Skotnicki
Nov 14 '18 at 14:42
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@PiotrSkotnicki yeah, they kind of messed up
initializer_list
– bolov
Nov 14 '18 at 15:00
@PiotrSkotnicki yeah, they kind of messed up
initializer_list
– bolov
Nov 14 '18 at 15:00
add a comment |
1 Answer
1
active
oldest
votes
The comment by @PiotrSkotnicki:
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.
Clarifies the problem.
An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:
EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}
Called as:
Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);
Live example HERE.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302655%2fc-how-to-use-emplace-back-for-user-defined-structure%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
The comment by @PiotrSkotnicki:
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.
Clarifies the problem.
An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:
EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}
Called as:
Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);
Live example HERE.
add a comment |
The comment by @PiotrSkotnicki:
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.
Clarifies the problem.
An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:
EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}
Called as:
Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);
Live example HERE.
add a comment |
The comment by @PiotrSkotnicki:
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.
Clarifies the problem.
An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:
EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}
Called as:
Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);
Live example HERE.
The comment by @PiotrSkotnicki:
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.
Clarifies the problem.
An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:
EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}
Called as:
Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);
Live example HERE.
answered Nov 14 '18 at 16:13
Bob__Bob__
5,01731425
5,01731425
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302655%2fc-how-to-use-emplace-back-for-user-defined-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
5
Your first argument to your constructor is
std::initializer_list<int>
yet you pass{ {2}, 1 }
?– CoryKramer
Nov 14 '18 at 14:38
I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?
– Programmer
Nov 14 '18 at 14:41
4
emplace_back
is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Usestd::initializer_list<int>{ 2, 1 }
.– Piotr Skotnicki
Nov 14 '18 at 14:42
@CoryKramer what is wrong with list-initializing the first int?
– Piotr Skotnicki
Nov 14 '18 at 14:44
@PiotrSkotnicki yeah, they kind of messed up
initializer_list
– bolov
Nov 14 '18 at 15:00