2

Given:

typedef boost::mpl::vector<Type1, Type2, Type3> types;
const size_t numTypes = boost::mpl::size<types>::value;
std::array<std::function<bool(const obj&, const obj&)>, numTypes*numTypes> arr;

I'm trying to get this sort of functionality in compile time:

for( size_t i = 0; i < numTypes; ++i )
{
    for( size_t j = 0; j < numTypes; ++j )
    {
        arr[i*numTypes+j] = ObjPair<boost::mpl::at_c<vecType, i>::type, boost::mpl::at_c<vecType, j>::type>::Foo;
    }
}

I think it would look something like:

std::array<std::function<bool(const obj&, const obj&)>, numTypes*numTypes> arr = { BOOST_PP_FOR((0, numTypes), PRED, OP, MACRO) };

But I can't get it working (I didn't post my full failed attempt at using BOOST_PP_FOR).

ObjPair<T1, T2>::Foo is a static method of signiture bool(const obj&, const obj&). It's specialized for different obj types.

I would be using this array to find a particular function given pairs of objects. The objects are held as their base classes, and I can index the array with some math to determine the index based on IDs available in the base class.

1

1 Answer 1

2

It is not possible for PP to iterate over boost::mpl::vector size. Hovewer you can try define it.

typedef boost::mpl::vector<bool, short, long> vecType;
#define numTypes 3

I have no TR1 so I try with boost array:

typedef  boost::function<bool(const obj&, const obj&)> Function;
typedef boost::array<Function, numTypes*numTypes> FooArray;

#define OBJPAIR_FOO_ARRAY(z, n, text)  BOOST_PP_COMMA_IF(n) &ObjPair<      \
boost::mpl::at_c<vecType, n/numTypes>::type,  \
boost::mpl::at_c<vecType, n%numTypes>::type   \
    >::Foo

FooArray fooArray= {
    BOOST_PP_REPEAT( BOOST_PP_MUL(numTypes, numTypes) , OBJPAIR_FOO_ARRAY, )
};
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! How can I use boost::mpl::size<types>::value instead of a hardcoded 3 in #define numTypes 3 - Why does everything except an integer literal cause compile errors there?
BOOST_PP_REPEAT need a text 3 witch is glued to somthing like REPEAT_TIMES_3 to expand into other makro like REPEAT_TIMES_2. Anything except literal 3 is not going to work.
Is there a way to use BOOST_PP_SLOT instead of 3? I wish this code was remotely debug-able, I can't even figure out the compile errors.
You may use whatever expands to 3. However it is impossible to expands any macro to size of boost::mpl::vector. You may define a BOOST_PP sequence and expand it to boost::mpl::vector typedef.
The class std::initializer_list<> is a first-class C++11 standard library type. However, they can be initially constructed statically by the C++11 compiler only through the use of the {} syntax. This Is Why We Can't Have Nice Things.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.