Using 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
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.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 10
Using std::move
Veeg om het menu te tonen
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
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.
Bedankt voor je feedback!