2

Let's say I have a boost::mpl::list< A, B, C ...>.

How do I access one of those types given an index value at runtime? Is it even possible?

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • 1
    Please provide some context of what you're trying to accomplish. – Emile Cormier May 13 '11 at 18:47
  • May be you'll find this question useful: http://stackoverflow.com/questions/4798169/is-there-a-way-to-break-out-of-boostmpl-for-each – Nim May 13 '11 at 19:53
  • @Emile Cornier> Whatever the context, mpl::list is a type container that provide compile-time type-container semantic. So the questions is clear : I got this compile-time list of types and I want to get the type that is at a specific index, but I got the index only at runtime, not compile-time. – Klaim May 13 '11 at 20:26

1 Answers1

2

http://www.boost.org/doc/libs/release/libs/mpl/doc/refmanual/for-each.html

you basically have to iterate over the entire list and introduce some sort of conditional: eg:

struct F {
    void operator(T &t) {
        if (i_ == index) ...
        ++i;
    }
    int index = ...;
    int i_ = 0;
};
for_each< L >( F(index) );
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • I don't see how that bare link answers the question. – Rob Kennedy May 13 '11 at 18:54
  • You know what? I posted this question after having checked all possibilities, and I had to leave my workplace. Once out I got this exact idea. TT___TT I guess I was too tired. Thanks anyway, that confirm it. – Klaim May 13 '11 at 20:24