bob*_*obo 5 c++ templates metaprogramming
所以,我正在尝试更好地学习模板元编程,我认为这是一个很好的练习.
我正在尝试编写可以使用我传递给它的任意数量的参数回调函数的代码.
// First function to call int add( int x, int y ) ; // Second function to call double square( double x ) ; // Third func to call void go() ;
回调创建代码应如下所示:
// Write a callback object that
// will be executed after 42ms for "add"
Callback<int, int, int> c1 ;
c1.func = add ;
c1.args.push_back( 2 ); // these are the 2 args
c1.args.push_back( 5 ); // to pass to the "add" function
// when it is called
Callback<double, double> c2 ;
c2.func = square ;
c2.args.push_back( 52.2 ) ;
我在想的是,使用模板元编程我希望能够声明回调,比如写一个这样的结构(请记住这是非常PSEUDOcode)
<TEMPLATING ACTION <<ANY NUMBER OF TYPES GO HERE>> >
struct Callback
{
double execTime ; // when to execute
TYPE1 (*func)( TYPE2 a, TYPE3 b ) ;
void* argList ; // a stored list of arguments
// to plug in when it is time to call __func__
} ;
因此,当被召唤时
Callback<int, int, int> c1 ;
您将通过<HARDCORE TEMPLATING ACTION>结构自动为您构建
struct Callback
{
double execTime ; // when to execute
int (*func)( int a, int b ) ;
void* argList ; // this would still be void*,
// but I somehow need to remember
// the types of the args..
} ;
在正确的方向上任何指针开始写这个?