Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio 2013 c++ lambda capture parameter pack

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);
}

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: 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();
}
like image 625
user3705549 Avatar asked Jun 04 '14 05:06

user3705549


1 Answers

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.

like image 79
user3705549 Avatar answered Sep 17 '22 07:09

user3705549