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.

David
  • 27,652
  • 18
  • 89
  • 138
  • 1
    For doing this directly in C++11, see http://stackoverflow.com/questions/2978259/programmatically-create-static-arrays-at-compile-time-in-c – TemplateRex Apr 24 '12 at 14:45

1 Answers1

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, )
};
Arpegius
  • 5,817
  • 38
  • 53
  • Thanks! How can I use `boost::mpl::size::value` instead of a hardcoded `3` in `#define numTypes 3` - Why does everything except an integer literal cause compile errors there? – David Apr 19 '12 at 00:42
  • 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. – Arpegius Apr 19 '12 at 01:53
  • 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. – David Apr 19 '12 at 02:13
  • 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. – Arpegius Apr 19 '12 at 02:24
  • `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 – Arpegius Apr 19 '12 at 03:13