#define FOR_EACH(OBJECT, CONTAINER, CONTAINER_TYPE) for (CONTAINER_TYPE::iterator OBJECT = CONTAINER.begin(); OBJECT != CONTAINER.end(); ++OBJECT)
Eh. You could do it with a template function. It may be a bit more verbose, but using a macro is asking for trouble. STL has a for_each( ) template function.
Quote:Original post by microdot
Eh. You could do it with a template function. It may be a bit more verbose, but using a macro is asking for trouble. STL has a for_each( ) template function.
With template functions, you need function pointers. You can't use it as you would a for_each in C#.
Quote:Original post by thedevdan
With template functions, you need function pointers.
No you don't, for_each doesn't have a parameter of type pointer to function, it has a parameter in which you can pass a free function, member function virtual & non-virtual, functional objects (functors) but it doesn't use function pointer as it's parameter.
Quote:Original post by snk_kidQuote:Original post by thedevdan
With template functions, you need function pointers.
No you don't, for_each doesn't have a parameter of type pointer to function, it has a parameter in which you can pass a free function, member function virtual & non-virtual, functional objects (functors) but it doesn't use function pointer as it's parameter.
Member function pointers need to be bound with mem_fun_ref etc, IIRC.
C++ doesn't have lambdas. Therefore, it doesn't have convenient for_each. Live with it, don't fight against it. Or, if you do fight against it, fight against it with something powerful.
If you really want to live on the edge, try this.
std::vector<int> numbers = ...;int q;FOR_EACH(int& i, numbers) { i=q++; if (q > 10) break; // yes, this works}
The evilness doesnt come anywhere near the depths of evilness of cats or coffee (hot choclate rules!)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement