如果不花很长时间来审查boost源代码,有人可以快速了解一下boost bind的实现方式吗?
我开始使用C++ 11 lambdas开发应用程序,并且需要将某些类型转换为函数指针.这在GCC 4.6.0中完美运行:
void (* test)() = []()
{
puts("Test!");
};
test();
Run Code Online (Sandbox Code Playgroud)
我的问题是当我需要在lambda中使用函数或方法局部变量时:
const char * text = "test!";
void (* test)() = [&]()
{
puts(text);
};
test();
Run Code Online (Sandbox Code Playgroud)
G ++ 4.6.0给出了强制转换错误代码:
main.cpp: In function 'void init(int)':
main.cpp:10:2: error: cannot convert 'main(int argc, char ** argv)::<lambda()>' to 'void (*)()' in initialization
Run Code Online (Sandbox Code Playgroud)
如果使用auto,它可以正常工作:
const char * text = "Test!";
auto test = [&]()
{
puts(text);
};
test();
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何用[&]为lambda创建一个类型?在我的情况下,我不能使用STL std :: function(因为我的程序不使用C++ RTTI和EXCEPTIONS运行时),并且它有一个简单的函数实现来解决这个问题?