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