
Another question about the to-be-released FOREACH macro, which I find I am using heavily... (BTW - Thanks Eric for your quick responses, and for the cool macro!) The issue is declaring the 'loop variable' for maps. For example, vector<int> v; ... FOREACH (int a, v) { ... } // this compiles int b; FOREACH (b, v) {...} // this also compiles However, for maps, the second variation (declaring outside the macro) doesn't work: map<int,int> m; ... FOREACH (map<int,int>::value_type i, m) {...} // compiles map<int,int>::value_type i; FOREACH (i, m) {...} // COMPILE ERROR Specifically, the compiler says error: non-static const member ` const int std::pair<const int,int>::first', can't use default assignment operator (from GCC 3.3.1). Despite my best efforts, I can't figure out what this error means, or what it's talking about. Anybody else run into this? Any thoughts? ---------------------------------------------------------------------- Dave Steffen, Ph.D. "The only justification for our concepts and Software Engineer IV systems of concepts is that they serve to Numerica Corporation represent the complex of our experiences; ph (970) 419-8343 x27 beyond this they have no legitimacy." fax (970) 223-6797 -- Albert Einstein dgsteffen@numerica.us

Dave Steffen wrote:
The first (key) member of the value_type for a map is const, so it can't have a default assignment operator. This issue doesn't arise if you declare the loop variable inside the loop because it's constructed and destroyed on each pass rather than being assigned to.
Anybody else run into this? Any thoughts?
You could make i a reference to value_type if you don't need to copy the map elements, or you could declare it as pair<int, int> (without the const) instead. Ben.

A map's value_type is defined as: std::pair<const key, value> Since the .first member is const, and pair doesn't have an overloaded assignment operator, you can't assign to it, as the compiler can't generate the operator automatically because it can't assign to .first. You'll have better luck using std::pair<int, int> in place of map<int,int>::value_type, hence pair<int,int> i; FOREACH (i, m) {...} should work.
HTH Pablo Aguilar

Dave Steffen schrieb:
this also doesn't work: map<int,int>::value_type i; i=std::pair<int,int>(1,2); // error that's because map<Key,Value>::value_type is defined to std::pair<Key /**/const/**/,Value>. and that's probably because you're not allowed to change the key of a map entry when you're e.g. iterating over the map. just use std::pair<int,int> instead and your example compiles. -- Stefan Strasser

Thanks very much for all your replies. I wrote
And the various replies are right on the money, and appreciated. I thought it was something like this, but I couldn't quite figure out what was going on. If anyone's interested: I'm playing with the idea of extending FOREACH in a way so that, instead of having a pair<>, you've got two separate variables for key and value. Maybe something like: map<int,int> m; ... FOREACH_PAIR(int key, int value, m) { ... } I'm having a very moderate amount of success. Anyone else playing with this sort of thing? Thanks again! ---------------------------------------------------------------------- Dave Steffen, Ph.D. "The only justification for our concepts and Software Engineer IV systems of concepts is that they serve to Numerica Corporation represent the complex of our experiences; ph (970) 419-8343 x27 beyond this they have no legitimacy." fax (970) 223-6797 -- Albert Einstein dgsteffen@numerica.us

Eric Niebler writes:
Ah, yes, this is the sort of thing I was after. I'll look into it. Every loop I've written for the past two weeks has turned out to be an iteration over a map of some sort; they are, now, all FOREACH loops. Being the lazy sort of programmer I am, I'm now looking for ways to customize FOREACH to make iteration over maps easier. Thanks very much! ---------------------------------------------------------------------- Dave Steffen, Ph.D. "The only justification for our concepts and Software Engineer IV systems of concepts is that they serve to Numerica Corporation represent the complex of our experiences; ph (970) 419-8343 x27 beyond this they have no legitimacy." fax (970) 223-6797 -- Albert Einstein dgsteffen@numerica.us
participants (7)
-
Ben Hutchings
-
Dave Steffen
-
Eric Niebler
-
Martin
-
Pablo Aguilar
-
Russell Hind
-
Stefan Strasser