C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pushing back a new struct on a list/vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
eriwik@student.chalmers.s
Guest





PostPosted: Wed Nov 22, 2006 10:10 am    Post subject: Pushing back a new struct on a list/vector Reply with quote



Consider this:

#inlcude <list>

struct elem {
int col;
int val;
};

int main() {
std::list<elem> l;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem? What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization. It's not
particulary important, it's just been bugging me for some time now.

--
Erik Wikström
Back to top
mimi
Guest





PostPosted: Wed Nov 22, 2006 10:10 am    Post subject: Re: Pushing back a new struct on a list/vector Reply with quote



"eriwik (AT) student (DOT) chalmers.se 写é“:
"
Quote:
Consider this:

#inlcude <list

struct elem {
int col;
int val;
};

int main() {
std::list<elem> l;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem?
I think adding a constructor is required. You could use std::pair, but

it provides a constructor also, not by you, but by the STL.
Quote:
What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization. It's not
particulary important, it's just been bugging me for some time now.

--
Erik Wikström
Back to top
Alf P. Steinbach
Guest





PostPosted: Wed Nov 22, 2006 10:10 am    Post subject: Re: Pushing back a new struct on a list/vector Reply with quote



* eriwik (AT) student (DOT) chalmers.se:
Quote:
Consider this:

#inlcude <list

struct elem {
int col;
int val;
};

int main() {
std::list<elem> l;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem?

No. And why would you want to?


Quote:
What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization.

You might consider using std::pair, or (not yet standard) std::tr1::tuple.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.