Visual studio 2013 c++ lambda捕获参数包

Visual studio 2013 c++ lambda capture parameter pack

本文关键字:参数 lambda studio 2013 c++ Visual      更新时间:2023-10-16

目前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在这里,以防其他人遇到这个问题并想知道状态。