这是我失败的尝试:
#define decltype(...) std::identity<decltype(__VA_ARGS__)>::type
template<typename T>
auto* degrade(const T& f) -> decltype(&T::operator())
{
return &T::operator();
}
int main()
{
std::array<void(int), 1> stuff =
{
degrade([](int){})
};
}
最佳答案
您在评论中说,
在这种情况下,您可以使用本地结构并在其中定义一个静态函数。 Something like this (well if it helps you):
#include <iostream>
void call(void (*f)(int))
{
for(int i = 0 ; i < 10 ; i++)
f(10 * i);
}
int main()
{
struct local
{
static void print(int i) { std::cout << i << std::endl; }
};
call(&local::print);
}
它很方便-或多或少像C++ 11 lambda。您可以在本地定义静态成员函数,然后将其传递给其他函数。
关于c++ - 如何在VS2010中将lambda降级为函数指针?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11511046/