I was wondering why std::move to a function with an rvalue parameter was not actually moving anything, but passing by reference instead?
Especially when I know it works for constructors.
I was running the following code:
#include <memory> #include <iostream> void consume_ptr(std::shared_ptr<int> && ptr) { std::cout << "Consumed " << (void*) ptr.get() << std::endl; } int main(int argc, char ** argv) { std::shared_ptr<int> ptr = std::make_shared<int>(); consume_ptr(std::move(ptr)); if (ptr) { std::cout << "ptr should be moved?" << std::endl; } return 0; } The output is:
ptr should be moved? According to everything I've read, the std::shared_ptr should have been moved inside the function, meaning that the object ptr itself would hold nullptr after moving it into consume_ptr, but it doesn't!
I tried it with some custom class of mine with logging, and it looks like the move constructor is never even called. It's reproducible under every compiler and optimization level.
Can anyone clear this up for me please?
shared_ptr. Even the humblestd::vectorhas a well defined state after being moved from: size shall be 0.