Search code examples
c++c++11move

Are there any pitfalls in using move() instead of std::move()?


When using new C++11's std::move(), I noted that, thanks to ADL, it's possible to just write move(), without the std:: namespace prefix.

Is using move() just fine? Are there any pitfalls? Is it just better to use std::move()?


Sample compilable code follows:

#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);
}

Solution

  • ADL will only find things in std if the argument types are also in std.

    If there is another symbol called move in another namespace that is being used, then the compiler won't know which you meant. There may not be one now, but future changes may bring one in.

    Additionally, always having the std:: makes it a lot easier for people reading your code later. It makes it clear that it's not a move that you've defined yourself or included from another library. Especially useful if they've not heard of the (relatively new) std::move before.