我可以从(旧式)函数签名中获取参数包吗?

dav*_*bak 0 c++ variadic-functions variadic-templates c++17

我想声明一个方法 - 可变参数 - 它的签名来自作为模板参数的“旧式”函数签名。

您可以std::function使用函数签名声明,例如,

std::function<int(float,float)> f;
Run Code Online (Sandbox Code Playgroud)

现在我想要一个模板,它采用这样的函数签名并以某种方式声明一个方法:

template <typename F>
struct Foo {
   [F's-return-type] Method(F's-Arg-pack]...) { ... }
};
Run Code Online (Sandbox Code Playgroud)

因此,如果您按如下方式实例化它,您将获得如下方法:

   Foo<int(float,float)> foo;
   int x = foo.Method(1.0f, 2.0f);
Run Code Online (Sandbox Code Playgroud)

或者也许有不同的方法来做到这一点?

Que*_*tin 7

您可以F使用非常简单的部分专业化进行反汇编:

template <class F>
struct Foo;

template <class Ret, class... Params>
struct Foo<Ret(Params...)> {
    Ret Method(Params...) { /* ... */ }
};
Run Code Online (Sandbox Code Playgroud)

在 Coliru 上现场观看