Visual studio 2013 c++ lambda捕获参数包
Visual studio 2013 c++ lambda capture parameter pack
目前Visual Studio 2013 update 2不支持完整的c++ 11,其中一个特性是在lambda中捕获参数包。是否有一种简单的方法来解决这个问题,或者我将不得不放弃visual studio并使用兼容的编译器,如mingw/g++?
下面的代码演示了我所想到的一个简单用例:
template <typename ... Args>
std::thread init_threaded(SomeObject sample, Args && ... args)
{
auto func = [=]()
{
sample->init(args...);
};
return std::thread(func);
}
这在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
编辑:这个错误似乎只发生在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();
}
正如评论中所讨论的,这是一个MSVC编译器错误,有一个解决方案。bug ticket在这里,以防其他人遇到这个问题并想知道状态。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何将lambda作为模板类的成员函数参数
- 使用自动推导的 lambda 参数作为常量表达式
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 引用捕获和在 lambda 中通过引用发送参数有什么区别 (C++)
- 将__device__ lambda 作为参数传递给 __global__ 函数
- 将 lambda 函数作为参数传递C++
- 如何将 lambda 函数作为参数发送到另一个函数
- Lambda可以用作非类型模板参数吗
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 如何确定捕获不可复制参数的 lambda 的类型?
- 省略C++可变参数 lambda 中的"auto"关键字?
- 如何访问可变参数 lambda 函数参数
- 可变参数 lambda 捕获的解决方法
- 传递给多个参数 lambda 表达式的参数
- 如何编写丢弃其参数的通用可变参数 lambda
- 使用c++参数lambda函数
- 将函数模板"pass"为泛型可变参数 lambda 返回语句是好方法吗?
- c++模板非类型参数lambda函数