 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
eriwik@student.chalmers.s Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Pushing back a new struct on a list/vector |
|
|
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
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Pushing back a new struct on a list/vector |
|
|
"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
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Pushing back a new struct on a list/vector |
|
|
* 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 |
|
 |
|
|
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
|
|