2012-04-18 3 views
2

사용 :컴파일 시간 초기화 감안할 때 부스트 PP 및 MPL

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; 

내가 컴파일 시간에 기능이 이런 종류의 얻으려고 :

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; 
    } 
} 

가 나는 그것이 같을 것이라고 생각을 :

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

하지만 그것은 (내가 BOOST_PP_FOR를 사용에서 내 전체 실패한 시도를 게시되지 않은) 작업을 얻을 수 없습니다.

ObjPair<T1, T2>::Foo은 서명의 정적 방법 bool(const obj&, const obj&)입니다. 다른 obj 유형에 특화되어 있습니다.

이 배열을 사용하여 특정 개체 쌍을 찾을 수 있습니다. 객체는 기본 클래스로 유지되며 일부 수학을 사용하여 배열을 인덱싱하여 기본 클래스에서 사용 가능한 ID를 기반으로 인덱스를 결정할 수 있습니다.

+1

참조 http://stackoverflow.com/questions/2978259/programmatically-create -static-arrays-at-compile-time-in-c – TemplateRex

답변

2

PP가 boost::mpl::vector 크기 이상으로 반복 할 수 없습니다. Hovewer 당신은 그것을 정의하려고 할 수 있습니다. 내가 부스트 배열을 시도 할 수 있도록

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

내가 더 TR1이 없습니다 :

C++ 11에서 직접이 작업을 수행하기위한
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,) 
}; 
+0

고마워요! '#define numTypes 3 '에서 하드 코딩 된'3' 대신'boost :: mpl :: size :: value'을 어떻게 사용할 수 있습니까? 왜 정수 리터럴을 제외한 모든 것이 에러를 컴파일합니까? – David

+0

BOOST_PP_REPEAT는 'REPEAT_TIMES_3'과 같이 텍스트 '3'마녀가 다른 마크로에 펼쳐지도록 붙일 필요가 있습니다. 리터럴 '3'을 제외한 모든 것은 작동하지 않습니다. – Arpegius

+0

3 대신 BOOST_PP_SLOT을 사용할 수 있습니까? 이 코드가 원격으로 디버그 가능하면 좋겠다. – David