简体   繁体   English

接受可变参数 lambda 时 std::function 的签名

[英]Signature of std::function when accepting a variadic lambda

Suppose I have a lambda and a class;假设我有一个 lambda 和一个类; I want to assign the lambda to the class as a static variable.我想将 lambda 作为静态变量分配给类。 How could I do that?我怎么能那样做?

auto func = [](void* p, auto&&... args) { /* do things */};
class A {
  static std::function<void(void*, ???)> f_;
};

I am not very sure what to put in the question marks.我不太确定在问号里放什么。 I tried我试过

class A {
  template<class...Arg>
  static std::function<void(void*, Arg...)> f_;
};

But when I assign like但是当我分配喜欢

A::f_ = func;

Then it is complaining about invalid use of incomplete type 'class std::function<void(void*, Arg ...)>'然后它抱怨invalid use of incomplete type 'class std::function<void(void*, Arg ...)>'

Please help!请帮忙!

std::function is a function wrapper; std::function是一个函数包装器; it is not a function template wrapper.它不是函数模板包装器。 It has to have a specific set of arguments.它必须有一组特定的参数。 You can do this for example for argument list int, double, float :例如,您可以为参数列表int, double, float

inline static std::function<void(void*, int, float, double)> f_ = func;

I want to assign the lambda to the class as a static variable.我想将 lambda 作为静态变量分配给类。

Then, perhaps it would be better to not wrap it in a std::function :然后,也许最好不要将它包装在std::function

inline static decltype(func) f_ = func;

But when I assign like但是当我分配喜欢

A::f_ = func;

Then it is complaining about ...然后就是抱怨...

This is because you didn't pass the template arguments to the template variable.这是因为您没有将模板参数传递给模板变量。 This would work:这会起作用:

A::f_<int, double, float> = func;

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

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