Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pushing back a new struct on a list/vector

Reply
Thread Tools

Pushing back a new struct on a list/vector

 
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      11-22-2006
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 Wikstrm

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-22-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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?


> 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:air, 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?
 
Reply With Quote
 
 
 
 
mimi
Guest
Posts: n/a
 
      11-22-2006

"(E-Mail Removed) 写道:
"
> 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:air, but
it provides a constructor also, not by you, but by the STL.
> 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


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      11-22-2006
On 22 Nov 2006 01:07:20 -0800 in comp.lang.c++,
"(E-Mail Removed)" <(E-Mail Removed)> wrote,
>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.


Sorry, no way to do that, but you can make it nearly anonymous by
limiting the scope of the very short-lived name.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
New releases: Pushing Daisies, Criminal Minds & Stop-Loss: Updated complete downloadable R1 DVD DB & info lists Douglas MacLean DVD Video 0 06-03-2008 07:11 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments