사용 :컴파일 시간 초기화 감안할 때 부스트 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를 기반으로 인덱스를 결정할 수 있습니다.
참조 http://stackoverflow.com/questions/2978259/programmatically-create -static-arrays-at-compile-time-in-c – TemplateRex