具有以下内容:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream f;
ifstream g;
f = std::move(g);
}
为什么 ifstream::operator=(const ifstream
&)
被调用而不是 ifstream::operator=(ifstream
&&)
即使调用了 std::move()
?
更新:一般来说,有没有办法将左值引用强制转换为右值引用?
最佳答案
你有什么证据表明正在调用 ifstream::operator=(const ifstream&)
?您是否收到编译错误提示您正在调用此私有(private)或已删除成员?
如果您的代码正在调用 ifstream::operator=(const ifstream&)
,并且如果您的实现声称是 C++11,那么这是您的 C++ std::库,或编译器。当我编译你的代码时,ifstream::operator=(ifstream&&)
被调用。这是设计使然。
为了确定,我在 ifstream::operator=(ifstream&&)
的实现中插入了一条打印语句。当我完成你的程序打印出来时:
basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
关于c++ - 存在 std::move 时未使用移动语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8210204/