简体   繁体   English

如何使用模板的扩展参数列表创建类型<typename… Args>功能?

[英]How to create a type with expanded argument list of template<typename… Args> function?

In general, I have a poor idea of how to create such a type:一般来说,我对如何创建这样的类型一无所知:

template<typename... Args>
using TWrapper = void *(*)(void *target, Args... func_args);

I understand why this is throwing errors, but I don't know what to do about it ...我明白为什么这会引发错误,但我不知道该怎么办......

I am developing a class with content something like this:我正在开发一个内容如下的类:

template<typename TFunc> //TFunc is a type of user's function
class CallManager {
    TFunc user_function = nullptr;
    void* inner_state; //For example

    template<typename... Args>
    using TWrapper = void *(CallManager::*)(void *const target, Args... func_args);

    TWrapper wrapper_from_another_instance = nullptr; //What do I need to specify in the template arguments if I don't know them?

    template <typename... Args> //I need to create a type for this function
    void* WrapUserFunction(void *const target, Args&&... func_args) {
        user_function(std::forward<Args>(func_args)...);
    }

    void setTargetExecutor(TWrapper wrapper) {
        wrapper_from_another_instance = wrapper;
    }
public:
    void setFunction(TFunc func) {
        user_function = func;
    }

    template <typename... Args>
    void makeCall(Args&&... func_args) {
        wrapper_from_another_instance(inner_state, std::forward<Args>(func_args)...);
    }

    void bindManager(const CallManager &next) { //Upd 1
        setTargetExecutor(next.WrapUserFunction);
    }
};

Using the example of the above class, it is possible to notice that I need an "extended" type in order to create a variable-pointer to a function for it, and then call it.使用上述类的示例,可能会注意到我需要一个“扩展”类型,以便为它创建一个指向函数的变量指针,然后调用它。

My class accepts a custom function type, but I don't know how many arguments there are and what type they are.我的班级接受自定义函数类型,但我不知道有多少个参数以及它们是什么类型。 Is it possible to somehow extract the list of arguments?是否有可能以某种方式提取参数列表?

How can I create a type for a function starting with " template<typename... Args> "?如何为以“ template<typename... Args> ”开头的函数创建类型?

How can I supplement the user's type with my own argument to save the result in variable for call?如何用我自己的参数补充用户的类型以将结果保存在变量中以供调用?

UPD 1: example of usage UPD 1:使用示例

using myfunc = void(*)(char x, char y);

void myFunc(char x, char y) {
}

void main() {
    CallManager<myfunc> cmgr1;
    cmgr1.setFunction(myFunc);

    CallManager<myfunc> cmgr2;
    cmgr2.bindManager(cmgr1);

    cmgr2.makeCall(10, 15);
}

If you limit you class to take function pointer, you might do:如果你限制你的类接受函数指针,你可能会这样做:

template<typename TFunc> //TFunc is a type of user's function
class CallManager;

template<typename R, typename... Args>
class CallManager<R (*)(Args...)>
{
    using TFunc = R (*)(Args...);
    using TWrapper = void *(CallManager::*)(void *const, Args...);
// ...
};

If you need to support more callable type, I suggest to create a " function_traits " in the same principe ( template specialization ) as helper.如果您需要支持更多的可调用类型,我建议在与 helper 相同的原则( 模板专业化)中创建一个“ function_traits ”。

Demo / Demo2演示/ DEMO2

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

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