use*_*549 6 c++ lambda c++11 visual-studio-2013
目前,Visual Studio 2013更新2不支持完整的C++ 11,其中一项功能是在lambda中捕获参数包.有没有一种简单的方法来解决这个问题,还是我不得不求助于放弃视觉工作室并使用兼容的编译器,如mingw/g ++?
以下代码演示了我想到的一个简单用例:
template <typename ... Args>
std::thread init_threaded(SomeObject sample, Args && ... args)
{
auto func = [=]()
{
sample->init(args...);
};
return std::thread(func);
}
Run Code Online (Sandbox Code Playgroud)
这在linux下的最新xcode(5.1.1)和最近版本的g ++(使用4.9.0)中效果很好但是在visual studio 2013 update 2中它给出了错误:
error C2536: 'init_threaded::<lambda_3a984affe0045c597607c0ec0a116b46>::init_threaded::<lambda_3a984affe0045c597607c0ec0a116b46>::<args_0>' : cannot specify explicit initializer for arrays
Run Code Online (Sandbox Code Playgroud)
编辑: 此错误似乎只发生在init函数中有不同类型时.以下示例无法编译.
#include <thread>
struct foo
{
void init(int arg1, std::string arg2) {}
};
template <typename ... Args>
std::thread init_threaded(foo *sample, Args && ... args)
{
auto func = [=]()
{
sample->init(args...);
};
return std::thread(func);
}
int main()
{
foo f;
auto t = init_threaded(&f, 1, "two");
t.join();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
874 次 |
最近记录: |