mam ten przykład:Dlaczego nie został wywołany konstruktor ruchu std :: string?
#include <string>
#include <iostream>
class Test {
private:
std::string str;
public:
Test(std::string &&str_) :
str(str_)
{}
const std::string &GetStr()
{
return str;
}
};
int main(int argc, char *argv[])
{
std::string there("1234567890");
std::cout << "1. there: " << there << '\n';
Test t1(std::move(there));
std::cout << "2. there: " << there << '\n';
std::cout << "3. there: " << t1.GetStr() << '\n';
}
Daje wyświetlamy
$ ./a.out
1. there: 1234567890
2. there: 1234567890
3. there: 1234567890
ta używa gcc 5.1.1 w systemie Linux. Podczas gdy ciąg there
pozostanie w ważnym, ale nieokreślonym stanie, po przeniesieniu, wydaje się, że ta implementacja przesuwa (i nie kopiuje) ciąg znaków, jeśli wywołany zostanie konstruktor ruchu std :: string.
jeśli mogę wymienić initalizer str(str_)
z str(std::move(str_))
uzyskać ten wynik:
$ ./a.out
1. there: 1234567890
2. there:
3. there: 1234567890
to sugerować ruch konstruktor std :: string jest obecnie wykorzystywana, ale dlaczego nie std::string(std::string &&)
jest wywoływana w moim pierwszym przykładzie?