Is it possible to implement this using templates preferably in a way that would work with vectors and arrays, but especially vectors.
-
The main functionality, used in PHP as:
foreach(objects as object) { object=this*that; } -
It would be nice to have the alternative usage as well if possible:
foreach(objects as key => object) { object=formula(key); }
=> is non essential. Templates aside, any technique, would be acceptable given this format.
C++0x supports range-based for:
but not many compilers support this yet (only gcc 4.6 have it AFAIK). Still, you could use
std::for_eachwith a lambda function like:If you don’t like C++0x, you could use Boost.Foreach like
For a
std::vector, there is no built-in support for the=>form. You need to keep the index yourself.