gpt4 book ai didi

c++ - Visual Studio 2013 c++ lambda 捕获参数包

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:48 25 4
gpt4 key购买 nike

目前 Visual Studio 2013 更新 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 编译器错误,并且有一个解决方法。故障单是 here以防其他人遇到此问题并想知道状态。

关于c++ - Visual Studio 2013 c++ lambda 捕获参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com