码迷,mamicode.com
首页 > 其他好文 > 详细

Reference Collapsing Rules, Universal Reference and the implementation of std::forward() and std::move()

时间:2016-02-29 01:57:49      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

关于reference collapsing,可以看这个链接。(里面也讲了std::forward()和std::move()的实现)

                http://thbecker.net/articles/rvalue_references/section_08.html

需要注意的是,在做auto-type deduction和template-type deduction的时候,在会有reference-stripping 发生:(来自scott meyers)

Note that when doing reference collapsing, reference would be stripped first:

           Things get subtler when deducing the type for a variable that is itself a reference. In that case, the reference part of the type is ignored.  For example, given

         int x;

         ...

         int&& r1 = 10;                   // r1’s type is int&&

         int& r2 = x;                     // r2’s type is int&

           the type for both r1 and r2 is considered to be int in a call to the template f.  This reference-stripping behavior is independent of the rule that, during type deduction for universal references, lvalues are deduced to be of type T& and rvalues of type T, so given these calls,

         f(r1);

         f(r2);

          the deduced type for both r1 and r2 is int&. Why? First the reference parts of r1’s and r2’s types are stripped off (yielding int in both cases), then, because each is an lvalue, each is treated as int& during type deduction for the universal reference parameter in the call to f.( from:  "Nitty Gritty Detail" of    https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers )

Reference Collapsing Rules, Universal Reference and the implementation of std::forward() and std::move()

标签:

原文地址:http://www.cnblogs.com/blog-of-walker/p/5226168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!