移动构造函数:C++ 中一种特殊的构造函数,用于从“将要被销毁/不再需要”的对象(右值或可移动对象)中转移资源所有权(如堆内存、文件句柄、缓冲区),以避免昂贵的深拷贝,提高性能。常见签名形如:T(T&& other)。(该术语主要用于 C++11 及以后)
/ˈmuːv kənˈstrʌktər/
A move constructor transfers resources from a temporary object instead of copying them.
移动构造函数会从临时对象转移资源,而不是进行拷贝。
When the vector grows, it may use the move constructor to relocate elements efficiently, reducing allocations and copy costs.
当 vector 扩容时,它可能使用移动构造函数高效地搬移元素,从而减少分配次数和拷贝开销。
“move”源自古法语 movoir(移动、推动),进一步来自拉丁语 movere(移动);“constructor”来自拉丁语 *construct-*(建造、构成)。在编程语境中,“constructor”指“构造函数”。“move constructor”作为术语与C++11 引入的移动语义(move semantics)紧密相关,用“move”来强调“资源转移”而非“复制”。
std::move 与性能陷阱