
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

Dave Steffen wrote:
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!) <snip> However, for maps, the second variation (declaring outside the macro) doesn't work:
map
m; ... FOREACH (map ::value_type i, m) {...} // compiles map ::value_type i; FOREACH (i, m) {...} // COMPILE ERROR Specifically, the compiler says
error: non-static const member ` const int std::pair
::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.
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

However, for maps, the second variation (declaring outside the macro) doesn't work:
map
m; ... FOREACH (map ::value_type i, m) {...} // compiles map ::value_type i; FOREACH (i, m) {...} // COMPILE ERROR Specifically, the compiler says
error: non-static const member ` const int std::pair
::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?
A map's value_type is defined as:
std::pair
---------------------------------------------------------------------- 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
HTH Pablo Aguilar

Dave Steffen schrieb:
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
m; ... FOREACH (map ::value_type i, m) {...} // compiles map ::value_type i; FOREACH (i, m) {...} // COMPILE ERROR Specifically, the compiler says
error: non-static const member ` const int std::pair
::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?
this also doesn't work:
map

Thanks very much for all your replies. I wrote
However, for maps, the second variation (declaring outside the macro) doesn't work:
map
m; ... FOREACH (map ::value_type i, m) {...} // compiles map ::value_type i; FOREACH (i, m) {...} // COMPILE ERROR
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

Dave Steffen wrote:
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
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?
Can't find the reference now, but someone suggested using tie() from the tuples library for this: int key, int value; BOOST_FOREACH(boost::tie(key, value), m) { ... } HTH, -- Eric Niebler Boost Consulting www.boost-consulting.com

Eric Niebler writes:
Dave Steffen wrote:
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
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?
Can't find the reference now, but someone suggested using tie() from the tuples library for this:
int key, int value; BOOST_FOREACH(boost::tie(key, value), m) { ... }
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