Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using std::move | Implementing Move Operations
C++ Move Semantics

bookUsing std::move

The utility std::move is an essential tool in modern C++ that enables move semantics. It is a standard library function that casts an lvalue (an object with a name and address) to an rvalue reference. By doing so, it allows move constructors or move assignment operators to be invoked, rather than their copying counterparts. However, it's important to understand that std::move does not move anything by itself—it simply makes it possible for move operations to occur.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream> #include <utility> #include <string> class Buffer { public: Buffer(size_t size) : size_(size), data_(new char[size]) { std::cout << "Constructed Buffer of size " << size_ << "\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; other.size_ = 0; std::cout << "Moved Buffer\n"; } // Move assignment operator Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; size_ = other.size_; other.data_ = nullptr; other.size_ = 0; std::cout << "Move-assigned Buffer\n"; } return *this; } // Destructor ~Buffer() { delete[] data_; std::cout << "Destroyed Buffer\n"; } // Deleted copy constructor and assignment Buffer(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete; void print() const { std::cout << "Buffer at " << static_cast<void*>(data_) << " size " << size_ << "\n"; } private: size_t size_; char* data_; }; int main() { Buffer a(10); Buffer b(20); std::cout << "Before move:\n"; a.print(); b.print(); // Move assignment: b takes resources from a b = std::move(a); std::cout << "After move:\n"; a.print(); b.print(); }

Use std::move when you no longer need the original object and want to transfer its resources. Do not use it on objects you intend to use afterward.

question mark

Which statements about std::move are correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you give an example of how to use std::move in code?

What happens if I use std::move on an object and then try to use it again?

When should I prefer std::move over copying?

bookUsing std::move

Scorri per mostrare il menu

The utility std::move is an essential tool in modern C++ that enables move semantics. It is a standard library function that casts an lvalue (an object with a name and address) to an rvalue reference. By doing so, it allows move constructors or move assignment operators to be invoked, rather than their copying counterparts. However, it's important to understand that std::move does not move anything by itself—it simply makes it possible for move operations to occur.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream> #include <utility> #include <string> class Buffer { public: Buffer(size_t size) : size_(size), data_(new char[size]) { std::cout << "Constructed Buffer of size " << size_ << "\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; other.size_ = 0; std::cout << "Moved Buffer\n"; } // Move assignment operator Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; size_ = other.size_; other.data_ = nullptr; other.size_ = 0; std::cout << "Move-assigned Buffer\n"; } return *this; } // Destructor ~Buffer() { delete[] data_; std::cout << "Destroyed Buffer\n"; } // Deleted copy constructor and assignment Buffer(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete; void print() const { std::cout << "Buffer at " << static_cast<void*>(data_) << " size " << size_ << "\n"; } private: size_t size_; char* data_; }; int main() { Buffer a(10); Buffer b(20); std::cout << "Before move:\n"; a.print(); b.print(); // Move assignment: b takes resources from a b = std::move(a); std::cout << "After move:\n"; a.print(); b.print(); }

Use std::move when you no longer need the original object and want to transfer its resources. Do not use it on objects you intend to use afterward.

question mark

Which statements about std::move are correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
some-alt