博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++11中auto&&是什么意思?
阅读量:6421 次
发布时间:2019-06-23

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

  hot3.png

By using auto&& var = <initializer> you are saying: I will accept any initializer regardless of whether it is an lvalue or rvalue expression and I will preserve its constness. This is typically used for forwarding (usually with T&&). The reason this works is because a "universal reference", auto&& or T&&, will bind to anything.

You might say, well why not just use a const auto& because that will also bind to anything? The problem with using a const reference is that it's const! You won't be able to later bind it to any non-const references or invoke any member functions that are not marked const.

As an example, imagine that you want to get a std::vector, take an iterator to its first element and modify the value pointed to by that iterator in some way:

auto&& vec = some_expression_that_may_be_rvalue_or_lvalue; auto i = std::begin(vec); (*i)++; This code will compile just fine regardless of the initializer expression. The alternatives to auto&& fail in the following ways:

auto => will copy the vector, but we wanted a reference auto& => will only bind to modifiable lvalues const auto& => will bind to anything but make it const, giving us const_iterator const auto&& => will bind only to rvalues So for this, auto&& works perfectly! An example of using auto&& like this is in a range-based for loop.

转载于:https://my.oschina.net/itfanr/blog/1791379

你可能感兴趣的文章
C#中三种截屏方式总结
查看>>
群发邮件功能的完善
查看>>
EF架构~LinqToEntity里实现left join的一对一与一对多
查看>>
Spring.net 学习笔记之ASP.NET底层架构
查看>>
C# System.Windows.Forms.WebBrowser中判断浏览器内核和版本
查看>>
Java 动态太极图 DynamicTaiChi (整理)
查看>>
Web APi之Web Host消息处理管道(六)
查看>>
微信公众平台后台编辑器上线图片缩放和封面图裁剪功能
查看>>
git使用教程2-更新github上代码
查看>>
张掖百公里,再次折戟
查看>>
SAP QM Batch to Batch的转移过账事务中的Vendor Batch
查看>>
本期最新 9 篇论文,帮你完美解决「读什么」的问题 | PaperDaily #19
查看>>
图解SSIS监视文件夹并自动导入数据
查看>>
Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
查看>>
人工智能将如何变革视频监控行业?
查看>>
MyBatis Review——开发Dao的方法
查看>>
阿里云容器宣布开放支持Kubernetes托管服务
查看>>
只在UnitTest和WebHost中的出现的关于LogicalCallContext的严重问题
查看>>
Linux_FTP服务器
查看>>
Django里自定义用户登陆及登陆后跳转到登陆前页面的实现
查看>>