use*_*882 3 c++ move-semantics
我编写了以下程序,并期望从中获得的右值std::move()在函数调用中使用后会立即被销毁:
struct A
{
A(){ }
A(const A&){ std::cout << "A&" << std::endl; }
~A(){ std::cout << "~A()" << std::endl; }
A operator=(const A&){ std::cout << "operator=" << std::endl; return A();}
};
void foo(const A&&){ std::cout << "foo()" << std::endl; }
int main(){
const A& a = A();
foo(std::move(a)); //after evaluation the full-expression
//rvalue should have been destroyed
std::cout << "before ending the program" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此。改为产生以下输出:
foo()
before ending the program
~A()
Run Code Online (Sandbox Code Playgroud)
正如答案中所说
右值表示在下一个分号处销毁的临时对象
我做错了什么?