Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > forwarding Args&&... vs forwarding Args...

Reply
Thread Tools

forwarding Args&&... vs forwarding Args...

 
 
Andrew Tomazos
Guest
Posts: n/a
 
      01-04-2012
Can someone please clarify the difference (if any) between the
following two functions...

template<class... Args>
void f1(Args&&... args)
{
f(forward<Args>(args)...);
}

and

template<class... Args>
void f2(Args... args)
{
f(forward<Args>(args));
}

Thanks,
Andrew.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-04-2012
On 1/4/2012 3:51 PM, Andrew Tomazos wrote:
> Can someone please clarify the difference (if any) between the
> following two functions...
>
> template<class... Args>
> void f1(Args&&... args)
> {
> f(forward<Args>(args)...);
> }
>
> and
>
> template<class... Args>
> void f2(Args... args)
> {
> f(forward<Args>(args));
> }


Take it with a grain of salt, I don't know much about 'forward'
template. The '&&' notation designates an rvalue reference, so it
narrows down [a little bit] how types 'Args' are deduced, *I guess*.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-04-2012
On 1/4/2012 3:51 PM, Andrew Tomazos wrote:
> Can someone please clarify the difference (if any) between the
> following two functions...
>
> template<class... Args>
> void f1(Args&&... args)
> {
> f(forward<Args>(args)...);
> }
>
> and
>
> template<class... Args>
> void f2(Args... args)
> {
> f(forward<Args>(args));
> }


Take it with a grain of salt, I don't know much about 'forward'
template. The '&&' notation designates an rvalue reference, so it
narrows down [a little bit] how types 'Args' are deduced, *I guess*.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Andrew Tomazos
Guest
Posts: n/a
 
      01-04-2012
On Jan 4, 9:51*pm, Andrew Tomazos <(E-Mail Removed)> wrote:
> Can someone please clarify the difference (if any) between the
> following two functions...
>
> template<class... Args>
> void f1(Args&&... args)
> {
> * * f(forward<Args>(args)...);
>
> }
>
> and
>
> template<class... Args>
> void f2(Args... args)
> {
> * * f(forward<Args>(args));
>
> }


Errata: The second function should read:

template<class... Args>
void f2(Args... args)
{
* * f(forward<Args>(args)...);

}

-Andrew.
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      01-04-2012
Andrew Tomazos wrote:

> Can someone please clarify the difference (if any) between the
> following two functions...
>
> template<class... Args>
> void f1(Args&&... args)
> {
> f(forward<Args>(args)...);
> }
>
> and
>
> template<class... Args>
> void f2(Args... args)
> {
> f(forward<Args>(args)...);
> }


int i;
f1(i); // Args... is int& (and so is Args&&...)
f2(i); // Args... is int
so with f2 you copied the argument.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      01-05-2012
On 4 Jan., 21:51, Andrew Tomazos wrote:
> Can someone please clarify the difference (if any) between the
> following two functions...
>
> template<class... Args>
> void f1(Args&&... args)
> { f(forward<Args>(args)...); }
>
> and
>
> template<class... Args>
> void f2(Args... args)
> { f(forward<Args>(args)); }


The difference (&&) affects template argument deduction. The first
function template always takes references as parameters and encodes
the value category of the supplied arguments as part of the type
parameters. This value category is then restored by std::forward. The
second function template always takes function parameters by value and
the forward call is basically just a move request (like std::move).

Keep in mind that something like

template<class T>
void foo(T&& x);

is basically a "catch everything and remember the value category, too,
so that it can be restored later"-signature.

int main() {
int i = 23;
// special deduction rule for T&& with lvalue arguments
// |
// | reference collapsing
// | |
foo(i); // --> deduce T=int& --> T&&=int&
foo(i+0); // --> deduce T=int --> T&&=int&&
}

As for restoring the value category:
forward<int >(...) yields an rvalue.
forward<int&>(...) yields an lvalue.

The use of std::forward is necessary because named rvalue references
are actually lvalues. The x in foo above is ALWAYS lvalue.

Cheers!
SG
 
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
Port forwarding problems with SP2 =?Utf-8?B?QW5keSBU?= Wireless Networking 1 03-29-2005 07:13 PM
T'Bird MAil Forwarding as file Peter Arnold Firefox 6 08-04-2004 05:19 PM
Forwarding mail Nobody Firefox 3 02-15-2004 05:09 PM
Forwarding mail Lomer Firefox 6 01-26-2004 05:16 PM
Mozilla 1.5 email and forwarding GFRfan Firefox 1 01-16-2004 04:39 AM



Advertisments