Search code examples
c++boostmetaprogrammingboost-mpl

How to get a type relative to a runtime index?


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?


Solution

  • 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) );