Ich stieß auf ein Youtube-Video auf C++ 11 Nebenläufigkeit (Teil 3) und den folgenden Code, der kompiliert und erzeugt das korrekte Ergebnis im Video.visuelle Studio-Implementierung von "move semantics" und "rvalue reference"
Allerdings habe ich einen Kompilierungsfehler dieses Codes mit Visual Studio 2012. Der Compiler beschwert sich über den Argumenttyp von . Wenn ich den Argumenttyp zu list<double>&
ändere, kompilierte der Code.
Meine Frage ist, was von move(list)
in _tmain()
zurückgegeben wird, ist es eine Rvalue-Referenz oder nur eine Referenz?
#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;
}
VS2012 ist zu alt, versuchen Sie mindestens VS2013 und vorzugsweise die Community-Vorschau für 2015 –