存在 std::move 时未使用移动语义
具有以下内容:
#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()
被调用?
更新:一般来说,有没有办法将左值引用强制转换为右值引用?
With the following:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream f;
ifstream g;
f = std::move(g);
}
Why is ifstream::operator=(const ifstream
&)
being called instead of ifstream::operator=(ifstream
&&)
even though std::move()
is called?
Update: Generally speaking, is there a way to coerce a lvalue reference to a rvalue reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有什么证据表明
ifstream::operator=(const ifstream&)
正在被调用?您是否收到编译错误,提示您正在调用此私有或已删除的成员?如果您的代码正在调用 ifstream::operator=(const ifstream&),并且您的实现声称是 C++11,那么这是您的 C++ std::lib 中的错误,或编译器。当我编译您的代码时,
ifstream::operator=(ifstream&&)
被调用。这是设计使然。为了确定起见,我在
ifstream::operator=(ifstream&&)
的实现中插入了一条 print 语句。当我执行你的程序时打印出:What evidence do you have that
ifstream::operator=(const ifstream&)
is being called? Do you get a compile error that says you're calling this private or deleted member?If your code is calling
ifstream::operator=(const ifstream&)
, and if your implementation is claiming to be C++11, then this is a bug in either your C++ std::lib, or compiler. When I compile your code,ifstream::operator=(ifstream&&)
gets called. And this is by design.I stuck a print statement in my implementation of
ifstream::operator=(ifstream&&)
just to be sure. When I did your program prints out:标准
我假设您正在查看错误的代码(没有任何地方可以保证必须调用基类运算符
istream::operator=(istream&&)
)?是的,这就是
std::move
的作用:还有一个
函数做同样的事情,前提是 move 构造函数不抛出异常。
Standard
I assume you are looking at the wrong code (nowhere is it guaranteed that the base class operator
istream::operator=(istream&&)
must be called)?Yes, it is what
std::move
does:There is also
which does the same, provided that the moveconstructor is nothrow.