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<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.
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.