在std :: move存在时移动未使用的语义

mos*_*ear 2 c++ noncopyable rvalue-reference c++11

具有以下内容:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream f;
    ifstream g;
    f = std::move(g);
}
Run Code Online (Sandbox Code Playgroud)

为什么被称为ifstream::operator=(const ifstream&)而不是ifstream::operator=(ifstream&&)即使std::move()被称为?

更新:一般来说,有没有办法强制左值引用到右值引用?

How*_*ant 5

你有什么证据ifstream::operator=(const ifstream&)可以被称为?您是否收到编译错误,说您正在调用此私有或已删除的成员?

如果您的代码正在调用ifstream::operator=(const ifstream&),并且您的实现声称是C++ 11,那么这是您的C++ std :: lib或编译器中的错误.当我编译你的代码时,ifstream::operator=(ifstream&&)会被调用.这是设计的.

ifstream::operator=(ifstream&&)为了确保我在执行中粘贴了一个print语句.当我打开你的程序时:

basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
Run Code Online (Sandbox Code Playgroud)