【发布时间】:2017-07-04 17:29:59
【问题描述】:
我无法编译下面的程序。
void toSin(std::list<double>&& list)
{
std::for_each(list.begin(), list.end(), [](double& x)
{
x = sin(x);
});
}
int main()
{
std::list<double> list;
const double pi = 3.141592;
const double epsilon = 0.0000001;
for (double x = 0.0; x < 2 * pi + epsilon; x = x + pi / 16)
{
list.push_back(x);
}
// Start thread
std::thread th(toSin, std::move(list));
th.join();
return 0;
}
我得到 > 错误 C2664: 'void (std::list<double,std::allocator<_Ty>> &&)' : 无法将参数 1 从 'std::list<double,std::allocator<_Ty>>' 转换为 'std::list<double,std::allocator<_Ty>> &&'
【问题讨论】:
-
无法复制。您使用的是哪个版本的视觉工作室?请注意,我添加了一堆缺失的标题。
-
std::thread th(toSin, std::move(list));行暗示你不应该在list上迭代超过那个点,因为它已经被移走了。但是您尝试在下一行对其进行迭代。 -
Visual Studio 2013
-
2013 缺少很多 C++ 11 支持。在 2015 年编译,但不应该按照您想要的方式运行。看看能不能升级。还要注意@FrançoisAndrieux 的警告。
-
除了 FrançoisAndrieux 评论,如果您希望使用相同的列表,您将拥有 UB,因为并发访问不同步(互斥,...)。