表现在下面的代码,在foo
断言失败:移动语义出乎意料
void bar (std::shared_ptr<int> && value) {
}
void foo() {
auto ptr = std::make_shared<int>(5);
bar(std::move(ptr));
assert(ptr == nullptr);
}
共享指针仍然指向呼叫到bar
后的值5。我期望拨打bar
的电话使用移动语义,并将ptr
留空。
我的理解缺陷在哪里?
你在哪里期待它移动到什么呢? – Galik
'std :: move()'声明:'template < class T > constexpr typename std :: remove_reference :: type && move(T && t);'返回值:'static_cast :: type &&>(t)'用法完美转发,通过引用或右值引用采用't',并将其转换为右值引用。 –