我看到了一段关于c++11并发(第三部分)的Youtube视频和下面的代码,这段代码能够在视频中被编译并生成正确的结果。
然而,当我使用Visual Studio 2012编译这段代码时,出现了一个编译错误。编译器抱怨toSin(list<double>&&)
的参数类型。如果我将参数类型更改为list<double>&
,则代码可以编译。
我的问题是,在_tmain()
中move(list)
返回的是rvalue引用还是普通引用?
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <list>
#include <algorithm>
using namespace std;
void toSin(list<double>&& list)
{
//this_thread::sleep_for(chrono::seconds(1));
for_each(list.begin(), list.end(), [](double & x)
{
x = sin(x);
});
for_each(list.begin(), list.end(), [](double & x)
{
int count = static_cast<int>(10*x+10.5);
for (int i=0; i<count; ++i)
{
cout.put('*');
}
cout << endl;
});
}
int _tmain(int argc, _TCHAR* argv[])
{
list<double> list;
const double pi = 3.1415926;
const double epsilon = 0.00000001;
for (double x = 0.0; x<2*pi+epsilon; x+=pi/16)
{
list.push_back(x);
}
thread th(&toSin, /*std::ref(list)*/std::move(list));
th.join();
return 0;
}