标签:
关于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 )
标签:
原文地址:http://www.cnblogs.com/blog-of-walker/p/5226168.html