tag:blogger.com,1999:blog-7101933101966798446.post465205308208316791..comments2024-02-07T08:27:42.210-08:00Comments on The View from Aristeia: "Universal References in C++11" now online at ISOCpp.orgScott Meyershttp://www.blogger.com/profile/05280964633768289328noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7101933101966798446.post-88471069059181680282012-11-06T18:52:03.271-08:002012-11-06T18:52:03.271-08:00Ah Ok. I see your point now. So, the function temp...Ah Ok. I see your point now. So, the function template under consideration is the one that takes a universal reference and not the two function templates that I listed in my comment.<br /><br />Yes, it is clear now. Thanks Scott!.Anubhavhttps://www.blogger.com/profile/15827657701005439897noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-67297288326831808572012-11-06T15:25:15.739-08:002012-11-06T15:25:15.739-08:00@Anubhav: When references such as r1 and r2 are pa...@Anubhav: When references such as r1 and r2 are passed as function arguments, the first thing that happens is that they're stripped of their referenceness. This is per 5/5: "If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis." <br /><br />In the calls to f, both r1 and r2 are treated the same way. Their types are first converted to int (per 5/5 above). Then 14.8.2.1/3 kicks in for f. f's parameter type is "is an rvalue reference to a cv-unqualified template parameter and the argument is an lvalue," so both r1 and r2 lead to a deduced type of int& for the template parameter T. From there, it's just reference-collapsing. So both f(r1) and f(r2) invoke f<int&>(int&).<br /><br />At least that's my understanding.Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-50162079712243881182012-11-06T01:56:36.343-08:002012-11-06T01:56:36.343-08:00Firstly, thanks for such a nice article. I had to ...Firstly, thanks for such a nice article. I had to read a few sections of the standard more than once to correlate with the concepts.<br /><br />In the section titled 'Nitty Gritty Details', I don't think I have understood the paragraph 'For example, given the type for both r1 and r2 is considered to be int in a call to the template f.'. Will appreciate if you could clarify this a bit more.<br /><br />Also, in the very next paragraph, I have understood it in the following way. Is it correct?<br /><br />r1 and r2 are both lvalue expresions. The function template under consideration are<br /><br />// for f(r1)<br />template void f(T &&t) <br />and <br />// for f(r2)<br />template void f(T &) <br /><br />respectively<br /><br />For the case of r1, therefore, 14.8.2.1 / 3 applies "If P is an rvalue reference to a cvunqualified template parameter and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction."<br /><br />Therefore T is int &, and the deduced A is int & &&, which is nothing but int & due to reference collapsing.<br /><br />For the case of r2, 14.8.2.1/3 applies - "If P is a reference type, the type referred to by P is used for type deduction."<br /><br />Therefore T is deduced as int, and the deduced A is int &.Anubhavhttps://www.blogger.com/profile/15827657701005439897noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-83014523445166784342012-11-03T07:50:53.778-07:002012-11-03T07:50:53.778-07:00I definitively like and share your concept of univ...I definitively like and share your concept of universal reference. It really helps practically. After watching your talk, I went back on all my code make use of && and found a lots of subtle errors, not only thanks to the notion of universal reference, but also because you describe how the reference collapsing rules work in C++<=11 with precision. Thanks a lots Scott!Anonymoushttps://www.blogger.com/profile/17210197569883727100noreply@blogger.com