简体   繁体   English

模板化函数指针?

[英]Templated function pointer?

I want to do something like this: I want the user to provide a return type and an argument (there will always only be one) then I want the user to be able to provide the pointer of a function that matches this criteria. 我想做这样的事情:我希望用户提供一个返回类型和一个参数(总是只有一个),然后我希望用户能够提供与该条件匹配的函数的指针。 I will be using this to create a timed event. 我将使用它来创建一个定时事件。 The issue here is that usually with templates you must provide T and make a new class instance, however in this case I need it kind of a runtime. 这里的问题是通常使用模板时,您必须提供T并创建一个新的类实例,但是在这种情况下,我需要使用它作为运行时。 ex: 例如:

TimeEvent *explode  = new TimeEvent(int (the return type),data (the argument), explodeFunc (the function pointer);

This would then create and set the function pointer. 然后将创建并设置函数指针。 Then the caller simply does explode.call() to call it. 然后,调用者只需执行explode.call()即可对其进行调用。 How could I achieve something like this? 我怎样才能实现这样的目标?

Thanks 谢谢

Well boost.function + boost.bind is something you can use for this: 那么boost.function + boost.bind可以用于此目的:

int explodeFunc( std::string const & someString ) {
     std::cout << someString << " exploded" << std::endl;
     return 1;
}

and later... 然后...

boost::function< int() > timeEvent = boost::bind(explodeFunc, "The world"); 
int retVal = timeEvent();

But I am not sure if this is what you are looking for 但我不确定这是否是您要寻找的

Here a simple version without boost: 这里是一个没有提升的简单版本:

#include <iostream>
#include <string>

template< typename R >
struct TimeEvent {
    virtual ~TimeEvent(){}
    virtual R call() = 0;
};

template< typename R, typename ParamType >
struct TimeEventT : TimeEvent<R> {
    typedef R (*callback_type)( ParamType const & );
    typedef ParamType param_type;
    TimeEventT( param_type const & param, callback_type cb )
        : TimeEvent<R>()
        , callback_( cb )
        , param_( param )
    {}

    R call() {
        return callback_( param_ );
    }

protected:
    callback_type callback_;
    param_type param_;
};

template< typename R, typename ParamType, typename ParamValueT >
TimeEvent<R> * create_time_event( 
    R (*cb)(ParamType const &),
    ParamValueT const & param
) {
    return new TimeEventT<R, ParamType>( param, cb );
}

int explodeFunc( std::string const & param ) { 
    std::cout << param << " exploded" << std::endl;
    return 1;
}

std::string explodeFuncString( std::string const & param ) { 
    return param + " really exploded this time";    
}

int main(){
    std::string param = "The world";
    TimeEvent<int> * timeEvent1 = create_time_event( explodeFunc, param );
    if( timeEvent1 ) {
        timeEvent1->call();
        delete timeEvent1;
    }
    TimeEvent<std::string> * timeEvent2 = create_time_event( explodeFuncString, param );
    if( timeEvent2 ) {
        std::cout << timeEvent2->call() << std::endl;
        delete timeEvent2;
    }
    return 0;
}

I hope you get the idea and can make it fit your needs. 希望您能想到,并使其适合您的需求。

HTH HTH

Edit: Updated with templated return type. 编辑:更新了模板化的返回类型。 * Made create_time_event a bit more user friendly *使create_time_event更加用户友好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM