【发布时间】:2014-05-21 10:57:26
【问题描述】:
在使用新的 C++11 的 std::move() 时,我注意到,由于 ADL,可以只编写 move(),而无需 std:: 命名空间前缀。
使用move() 好吗?有什么陷阱吗?使用std::move()会更好吗?
示例可编译代码如下:
#include <iostream>
#include <utility>
#include <vector>
template <typename T>
void print(const char* const descr, const std::vector<T>& v) {
std::cout << descr << ": ";
for (const auto& x : v) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
int main() {
std::vector<int> v{11, 22, 33};
std::vector<int> w = move(v);
print("v", v);
print("w", w);
}
【问题讨论】:
-
"
move()看起来比std::move()" 更干净。此外,如果您的move()需要做的不仅仅是std::move()并且需要由 ADL 找到,那么您的move()已损坏。