Move semantics and rvalue references
Copy semantics are for creating clones of objects. It is useful sometimes, but not always needed or even meaningful. Consider the following class that encapsulates a TCP client socket. A TCP socket is an integer that represents one endpoint of a TCP connection and through which data can be sent or received to the other endpoint. The TCP socket class can have the following interface:
class TCPSocket { public: TCPSocket(const std::string& host, const std::string& port); ~TCPSocket(); bool is_open(); vector<char> read(size_t to_read); size_t write(vector<char> payload); private: int socket_fd_; TCPSocket(const TCPSocket&); TCPSocket& operator = (const TCPSocket&); };
The constructor opens a connection to a host on a specified port and initializes the socket_fd_
member variable. The destructor closes the connection. TCP does not define a way to make clones of open sockets (unlike file descriptors with dup
...