我有一段C++代码如下:
template <typename ...A>
struct CastAll{
template <typename ...B>
void cast_all(void(*fun)(B...), A...as){
//...
}
};
Run Code Online (Sandbox Code Playgroud)
我想要做的是以这样的方式实现cast_all:它将每个参数动态转换为B中的相应类型,然后使用"casted"参数调用给定的函数fun.
例如,在:
struct A{};
struct B : public A{};
void foo(B *b1, B *b2){
//... does something with b1 and b2
}
int main(){
A *a1 = new B();
A *a2 = new B();
CastAll<B*, B*> cast; //used to cast each A* to B*
cast.cast_all<B*, B*>(foo, a1, a2);
}
Run Code Online (Sandbox Code Playgroud)
cast_all应扩展为:foo(dynamic_cast(a1),dynamic_cast(a2));
我看过很多关于可变参数模板的文章.然而,几个小时后,我仍然无法弄明白.
有任何想法吗?