Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template argument as rvalue reference

Reply
Thread Tools

Template argument as rvalue reference

 
 
SG
Guest
Posts: n/a
 
      10-28-2012
Am 27.10.2012 21:12, schrieb Juha Nieminen:
> SG <> wrote:
>>> If you have a non-templated rvalue reference parameter,
>>> you can't give it an lvalue. You'll get a compile error.

>>
>> What is an "rvalue reference parameter"?

>
> Something like this:
>
> void foo(int&& i);
>
> If you try to give an lvalue to that, it gives an error.


Right. The thing is, if you write

template<class T> void blah(T&& x) {...}

x is not necessarily an rvalue reference parameter. It looks like one.
And it may be one depending on what T is. But is also may be an lvalue
reference. The reference collapsing rule "&+&&=&" makes x an lvalue
reference in case T is an lvalue reference type, otherwise x is an
rvalue reference. That's reference collapsing.

In addition to reference collapsing we have a special template argument
deduction rule which automatically picks the "right T" to make
initializing a parameter of type T&& possible. That is, if the argument
was an lvalue, the corresponding template type parameter is deduced to
be an lvalue reference type, otherwise it is a non-reference type. Why?
Because these rules are the rules they came up with to enable "perfect
forwarding".

> I must admit, I do not know the new standard enough to understand what
> exactly is going on here, and why it makes a difference (and why you need
> a typedef):
>
>> typedef int& foo;
>>
>> void bar(foo&& x);


The syntax does not allow "int& && x". I guess that is because there
would be no point in allowing it since you could just as well write
"int& x".

If you havn't already seen one of Scott Meyers' latest talks about
rvalue references, I encourage you to take a look.

HTH,
SG

 
Reply With Quote
 
 
 
 
ptyxs
Guest
Posts: n/a
 
      10-29-2012
Le 27/10/2012 21:12, Juha Nieminen a écrit :
> SG <> wrote:
>
> I must admit, I do not know the new standard enough to understand what
> exactly is going on here


To modify this unsatisfactory situation, you just have to read the
references I gave in my preceding message :

Thomas Becker paper :

http://thbecker.net/articles/rvalue_...ection_01.html

and Scott Meyers paper in Overload 111 october 2012 :

http://accu.org/index.php/journals/c78/

Ptyxs


 
Reply With Quote
 
 
 
 
ptyxs
Guest
Posts: n/a
 
      10-30-2012
Le 27/10/2012 21:12, Juha Nieminen a écrit :
> SG <> wrote:
>
> I must admit, I do not know the new standard enough to understand what
> exactly is going on here


To modify this unsatisfactory situation, you just have to read the
references I gave in my preceding message :

Thomas Becker paper :

http://thbecker.net/articles/rvalue_...ection_01.html

and Scott Meyers paper in Overload 111 october 2012 :

http://accu.org/index.php/journals/c78/

Ptyxs


 
Reply With Quote
 
Zhihao Yuan
Guest
Posts: n/a
 
      11-20-2012
On Friday, October 26, 2012 5:23:15 AM UTC-5, Juha Nieminen wrote:
> template<typename T> void foo(T&&, T&&);


Let's stop repeating the materials all over the internet. The
following is exactly what you want:

template<typename T1, typename T2> void foo(T1&&, T2&&,
typename std::enable_if<std::is_same<
typename std::remove_reference<T1>::type,
typename std::remove_reference<T2>::type>::value>::type* = 0)
{}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
How does the compiler interpret a template argument (reference vs. non-reference) ? mrstephengross C++ 2 09-07-2005 03:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57