博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11: 右值引用 addition
阅读量:7235 次
发布时间:2019-06-29

本文共 1286 字,大约阅读时间需要 4 分钟。

std::move

class Node {public:     Node() {}        Node(const Node& nd) {        cout << "const Node& nd" << endl;    }    Node(Node&& nd) {        cout << "Node&& nd" << endl;    }};int main() {    const Node nd;    // unfortunitely, print "const Node& nd"     Node cy_nd(std::move(nd));    return 0;}

Because the move assignment operator takes an rvalue reference to a non-const std::string.

The rvalue can, however, be passed to the copy assignment operator,
because an lvalue reference-to-const is permitted to bind to a const rvalue.

std::forward

// const lvalue referencevoid inner(const int& i) {  cout << "int&" << endl; }// non-const rvalue referencevoid inner(int&& i) { cout << "int&&" << endl; } // universal referencetemplate
void outer(T&& i) { /*i is lvalue reference * print "int&" */ inner(i); /* using std::forward * if : i is rvalue reference * then : print "int&&" * else : still print "int&" */ inner(std::forward
(i));}int main() { int i = 2; outer(2); return 0;}

std::move unconditionally casts its argument to an rvalue,

std::forward does it under certain conditions.
std::forward is a conditional cast :
It casts to an rvalue only if its argument was initialized with an rvalue.

转载地址:http://sagfm.baihongyu.com/

你可能感兴趣的文章
异步消息的传递-回调机制(转)
查看>>
【OpenCV学习】图像的形态处理学
查看>>
深入学习GridBagLayout
查看>>
memcache实战之三 :客户端 enyim.com Memcached Client 的安装与调试以及实例,2012年下载地址...
查看>>
centos下安装tomcat
查看>>
Notification与NotificationManager
查看>>
Qt之布局管理--(2)多文档的布局和焦点
查看>>
go-tour练习解答
查看>>
句柄和指针的区别和联系是什么?
查看>>
linux Shell学习笔记第三天
查看>>
asp.net mvc本地程序集和GAC的程序集冲突解决方法
查看>>
可重入锁 RLOCK(转)
查看>>
DataTable排序结果的纠正
查看>>
关于中国天气(Weather.com.cn)的查询
查看>>
关闭磁盘自动运行
查看>>
分享简化您的设计过程的CSS网格系统
查看>>
awk使用技巧
查看>>
mvc 截取上传图片做头像,自动生成不同小尺寸缩略图
查看>>
AutoCAD 命令统计魔幻球的实现过程--(1)
查看>>
判断是大端字节序还是小端字节序
查看>>