Re: Compile time container, run-time execution.
shar3ub@googlemail.com wrote:
I was wondering if anyone could give me some directions.
I want to be able to build a compile time container of some all
objects of type 'X' that exist in the code, and then at run time,
iterate through the container and invoke a specific function on all
items in this container. Could template-metaprogramming help me here?
Metaprogramming probably has really nothing to do with that, but the
answer is "yes". See 'std::for_each'.
Is there something easier as I really don't have the time to spend on
learning all about template metaprogramming.
You don't need template metaprogramming.
It sounds pretty impossible for me to do, but if anyone has any
suggestions, I would highly appreciate them.
Pretty impossible? Well, we're not going to do it *for you*, if that's
what you're hoping for. Perhaps you need to make an attempt, and if you
run into some problems, ask particular questions. We'll be happy to
assist, but you need to make an effort.
I'll go as far as to make an example for you. It's up to you to make it
do what *you* need.
#include <iostream>
#include <vector>
#include <algorithm>
class A {};
void foo(A const& a)
{
std::cout << "foo called for A at address " << &a << "\n";
}
int main()
{
std::vector<A> va(10);
std::for_each(va.begin(), va.end(), foo);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask