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