当使用新的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()
就是有问题的。 - Griwes