Suppose I have a lambda and a class; I want to assign the lambda to the class as a static variable. 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 ...)>'
Please help!
std::function
is a function wrapper; 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
:
inline static std::function<void(void*, int, float, double)> f_ = func;
I want to assign the lambda to the class as a static variable.
Then, perhaps it would be better to not wrap it in a 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.