Лекция 3. Move семантика и Rvalue reference (1114995)
Текст из файла
Лекция 3. Move семантика иRvalue referenceValery Lesin. C++ In-Depth, 20141Быстрые программы• Программы на C++ ценят за их скорость1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.string inverted(string const& str){if (str.empty())return string();string tmp(str);size_t beg = 0;size_t end = tmp.size() - 1;while(beg < end)swap(tmp[beg++], tmp[end--]);return tmp;}//...string a = "olleH";a = inverted(a);• И сколько же дополнительных копированийстроки?Valery Lesin. C++ In-Depth, 20142Быстрые программы• Программы на C++ ценят за их скорость1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.string inverted(string const& str){if (str.empty())return string();string tmp(str);size_t beg = 0;size_t end = tmp.size() - 1;while(beg < end)swap(tmp[beg++], tmp[end--]);return tmp;}//...string a = "olleH";string b = inverted(a);• Три (или два при RVO)• Как избежать копирования из разрушающегося объекта?Valery Lesin.
C++ In-Depth, 20143Lvalue vs Rvalue• Lvalue – выражение, описывающее объект,живущий дольше самого выражения(мнемоника: то, что может быть по левуюсторону от ‘=’).• Rvalue – выражение, описывающеевременный объект (мнемоника: то, чтоможет быть по правую сторону от ‘=’).Valery Lesin. C++ In-Depth, 20144Lvalue vs Rvalue. Примеры1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.// L-valuestring answ = "42";answ = "13";//...int& answer_ref() {return answ;}answer_ref() = "7";Valery Lesin. C++ In-Depth, 20145Lvalue vs Rvalue.
Примеры1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.// L-valuestring answ = "42";answ = "13";//...string& answer_ref() {return answ;}answer_ref() = "7";// R-valuestring answer(){string answ = "7";return good_day() ? answ : "13";}string const& a = answer(); // works finestring&b = answer(); // nope!answer() = "13";// nope!Valery Lesin. C++ In-Depth, 20146Как отличить временныйобъект от невременного• Rvalue reference1.string&& a = answer(); // compiled, but rare code• Такая ссылка крайне полезна при перегрузке1.2.string low_case(string const& s); // need copystring low_case(string&&s); // need no copyValery Lesin. C++ In-Depth, 20147Move constructor1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.// copy constructorstring(string const& other): buf_ (new char[other.size() + 1]) // buf_ is 'char*', size_(other.size()){strcpy(buf_, other.buf_);}Valery Lesin. C++ In-Depth, 20148Move constructor1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.// copy constructorstring(string const& other): buf_ (new char[other.size() + 1]) // buf_ is 'char*', size_(other.size()){strcpy(buf_, other.buf_);}// move constructor, no need to copy in ‘string s = answer()’string(string&& other): buf_ (other.buf_ ) // stealing pointer, size_(other.size()){other.buf_ = nullptr;other.size_ = 0;}Valery Lesin.
C++ In-Depth, 20149Move constructor1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.// copy constructorstring(string const& other): buf_ (new char[other.size() + 1]) // buf_ is 'char*', size_(other.size()){strcpy(buf_, other.buf_);}// move constructor, no need to copy in ‘string s = answer()’string(string&& other): buf_ (other.buf_ ) // stealing pointer, size_(other.size()){other.buf_ = nullptr;other.size_ = 0;}// move constructor via delegating and swap (*)string(string&& other): string() // default constructor{swap(other);}Valery Lesin.
C++ In-Depth, 201410Move assignment operator1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.// swap trickstring& operator=(string const& other){string tmp(other);swap(tmp);return *this;}Valery Lesin. C++ In-Depth, 201411Move assignment operator1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.// swap trickstring& operator=(string const& other){string tmp(other);swap(tmp);return *this;}// extended swap trick gives// copy and move ‘op=’ at the same timestring& operator=(string other){swap(other);return *this;}Valery Lesin. C++ In-Depth, 201412Rvalue reference – Rvalue?• Что, если поле имеет пользовательский тип?1.2.3.4.string(string&& other): buf_(other.buf_) // buf_ is 'vector<char>'{ // doesn’t work as expected}• Здесь Rvalue reference – не Rvalue, т.к.
указывает наименованный невременный объект.1.2.3.4.5.6.7.void foo(T const& t){// has overloadings for T const& and T&&bar(t); // (1)//...// has overloadings for T const& and T&&qux(t); // (2)}• Если бы t был rvalue, то смена прототипа функции fooпринципиально меняла бы ее логику – вероятно, былобы падение в (2).Valery Lesin. C++ In-Depth, 201413std::move• Как объяснить компилятору, что объект все-такиrvalue?• move не ‘перемещает’ объект, а только изменяет еготип.1.2.3.4.5.6.7.8.9.template< class T >typename std::remove_reference<T>::type&& move(T&& t);//...string(string&& other): buf_(std::move(other.buf_)) // buf_ is 'vector<char>'{ // now works fine!}Valery Lesin.
C++ In-Depth, 201414Move constructor vs other constructors• Move семантика является родной для RAII– Копирование часто невозможно, а передачавладение допустима.– Пример: std::unique_ptr• Move конструктор ‘выключает’ автоматическуюгенерацию других конструкторов (но не в MSVC),но в С++11 есть удобный прием:1.2.3.4.5.6.struct data{data() = default;data(data&& other){/*...*/}//...};Valery Lesin. C++ In-Depth, 201415Возврат из функции значения vs &&• Rvalue reference – лишь ссылка.
Вернув ее навременный объект, получите undefinedbehavior.• Возвращайте значение! Контринтуитивно, да?Move семантика обеспечит ‘условнобесплатное’ копирование.• Можно вернуть && на поле (как и обычнуюссылку). Но тогда удаление поля будетзависеть от контекста вызова. Почему?Valery Lesin. C++ In-Depth, 201416Нужен ли теперь swap(T&, T&)?• Если ваш класс обладает move семантикой, тонет необходимости реализовывать отдельныйswap для него – подойдет обобщенный.1.2.3.4.5.6.7.template<class T>void swap(T& lhs, T& rhs){T tmp = move(lhs);lhs= move(rhs);rhs= move(tmp);}Valery Lesin.
C++ In-Depth, 201417Преобразование & <-> const & <-> &&1.2.implemented: void foo(T&);not implemented: void foo(T&&);• Можно вызвать для Lvalue, но не для Rvalue.1.2.implemented: void foo(T const&);not implemented: void foo(T&&);• Можно вызвать для Lvalue и для Rvalue, нонельзя их отличить.1.2.3.implemented: void foo(T&&);not implemented: void foo(T&);not implemented: void foo(T const&);• Можно вызвать для Rvalue, а вот вызов дляLvalue даст compile error.Valery Lesin. C++ In-Depth, 201418Perfect forwarding (*)• В пример с функцией inverted по-прежнемуосталось лишнее копирование при передачепараметра. Что делать?1.2.3.4.5.6.7.8.template<class string_t>string inverted(string_t&& str){string tmp(std::forward<string_t>(str));//...// or even much easierstring inverted(string str);• Вызов inverted(str) приведет к копированию,inverted(move(str)) – к перемещению.• Функция std::forward крайне полезна для variadictemplates.• Поможет с конструктором?Valery Lesin.
C++ In-Depth, 201419Вопросы?Valery Lesin. C++ In-Depth, 201420.
Характеристики
Тип файла PDF
PDF-формат наиболее широко используется для просмотра любого типа файлов на любом устройстве. В него можно сохранить документ, таблицы, презентацию, текст, чертежи, вычисления, графики и всё остальное, что можно показать на экране любого устройства. Именно его лучше всего использовать для печати.
Например, если Вам нужно распечатать чертёж из автокада, Вы сохраните чертёж на флешку, но будет ли автокад в пункте печати? А если будет, то нужная версия с нужными библиотеками? Именно для этого и нужен формат PDF - в нём точно будет показано верно вне зависимости от того, в какой программе создали PDF-файл и есть ли нужная программа для его просмотра.