【问题标题】:std::function with templated Argument Types具有模板化参数类型的 std::function
【发布时间】:2018-01-25 20:39:30
【问题描述】:
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);

但编译器抱怨“不允许类型名”。

【问题讨论】:

  • 请发minimal reproducible example 重现错误
  • @Praetorian 这是一个完整的例子。
  • 不,不是。除了缺少标头包含和特定于操作系统的OutputDebugStringA 之外,发布的示例确实可以编译。您应该发布重现错误消息的版本。如果你问我认为你在问什么,答案是函数指针与std::function不同
  • 不,它不是:完整的示例需要 main() 和包含

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多