문제

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.

도움이 되었습니까?

해결책

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, )
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top