I’d like to get my mucky paws on the operator() of a lambda function. The following seems up the task:
template <typename F>
void bar(F func) {
void (F ::*pm)();
pm = &F::operator();
}
However, in the following, I need to include the mutable keyword. Why is that? Is it possible to above instead declare a pointer to member function, which can target arbitrary lambdas?
int main(int argc, char *argv[])
{
bar([]() mutable {});
return 0;
}
According to 5.1.2 of the N3291 C++0x specification, the lambda’s operator() is
constunless you explicitly declare itmutable:You may be able to do some template metaprogramming magic to detect which is which.
However, it should be noted that, once
funcgoes out of scope, you can’t use that member pointer anymore. And it is a member pointer, not a function pointer, so you can’t convert between the two.