std :: function with templated Argument Types

时间:2018-01-25 20:39:30

标签: c++ c++11 templates std-function

struct Functor
{
public:
    template <typename... argtypes>
    Functor(std::function<void()> Function)
    {
        this->Function = Function;
    }

    ~Functor() {}

    void operator()() const
    {
        OutputDebugStringA("Hello there, General Kenobi");
        Function();
    }

private:
    std::function<void()> Function;
};

void gtk()
{
    OutputDebugStringA("What is happening to meeee");
}
Functor Draw = Functor(&gtk);
void foo() { Draw(); }

如何让Functor的std::function接受参数类型? 我尝试了以下方法:

Functor(std::function<void(argtypes...)> Function)
Functor Draw = Functor<void>(&gtk);

但编译器抱怨'typename not allowed'。

1 个答案:

答案 0 :(得分:1)

你需要让Functor本身成为一个模板,而不仅仅是它的构造函数。参数是调用约定的一部分,因此需要的范围比ctor更广泛。 std :: function成员也需要参数类型,并且在实际调用存储的可调用对象时也需要它们。

相关问题