开发者

How to create an array to hold C++ classes (not instances) to be used iteratively in a template function

I need to repeatedly make calls to a template function wi开发者_运维问答th different classes defined elsewhere in the code, like so:

MyTemplateFunction<ClassOne>( &AnotherTemplateFunction<ClassOne> );
MyTemplateFunction<ClassTwo>( &AnotherTemplateFunction<ClassTwo> );
MyTemplateFunction<ClassThree>( &AnotherTemplateFunction<ClassThree> );
MyTemplateFunction<ClassFour>( &AnotherTemplateFunction<ClassFour> );

Is there a way to create an array of the specializations for the classes ClassOne, ClassTwo etc. so I can simply iterate over the array for better maintainability.

edit: I am specifically using the register_exception_translator function in Boost.Python. So I don't really have a choice. Its a third party function I have to call for all of my classes, which are over 50 in number in my project. Repeating the calls like this has been a messy experience when adding or modifying classes.


You should look into Boost.MPL, specifically sequences.


Something like this should suffice

template<typename I, typename N> struct cons { };
struct nil { };

template<typename T, typename U>
void call(cons<T, U>) {
  MyTemplateFunction<T>(&AnotherTemplateFunction<T>);
  call(U());
}

void call(nil) { }

typedef cons<ClassA, cons<ClassB, cons<ClassC, nil> > > conses;
int main() {
  call(conses());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜