Visual studio 2013 c ++ lambda捕获参数包 [英] Visual studio 2013 c++ lambda capture parameter pack

查看:210
本文介绍了Visual studio 2013 c ++ lambda捕获参数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前Visual Studio 2013更新2不支持完整的C ++ 11,其中的一个功能是捕获lambda中的参数包。有没有一个简单的方法来解决这个问题,或者我将不得不求助于ditching视觉工作室和使用兼容的编译器,如mingw / g ++?

Currently Visual Studio 2013 update 2 does not support full C++11, one of those features is capturing parameter packs in a lambda. Is there an easy way to work around this or will I have to resort to ditching visual studio and using a compliant compiler such as mingw/g++?

下面的代码演示了一个简单的使用情况下我的想法:

The following code demonstrates a simple use case of what I had in mind:

template <typename ... Args>
std::thread init_threaded(SomeObject sample, Args && ... args)
{
  auto func = [=]()
  {
    sample->init(args...);
  };

  return std::thread(func);
}

这在最新的xcode(5.1.1) g ++(使用4.9.0)在linux下,但在visual studio 2013更新2它给出错误:

This works great in the latest xcode (5.1.1) and recent versions of g++ (using 4.9.0) under linux however in visual studio 2013 update 2 it gives the error:

error C2536: 'init_threaded::<lambda_3a984affe0045c597607c0ec0a116b46>::init_threaded::<lambda_3a984affe0045c597607c0ec0a116b46>::<args_0>' : cannot specify explicit initializer for arrays

Edit:
这个错误似乎只发生在init函数中有不同类型的时候。以下示例不编译。

This error seems to only happen when there are different types in the init function. The following example does not compile.

#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编译器错误,有一个工作。错误凭单是这里,以防其他人遇到这种情况,并想知道状态。

As discussed in the comment this is a MSVC compiler bug and there is a work around. The bug ticket is here in case anyone else runs into this and wants to know the status.

这篇关于Visual studio 2013 c ++ lambda捕获参数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆