Article Discussion
A Brief Introduction to Rvalue References
Summary: Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higher performance and more robust libraries.
64 posts on 5 pages.      
« Previous 1 2 3 4 5 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: December 16, 2014 10:40 AM by Roman
Walter
Posts: 12 / Nickname: wkaras / Registered: December 22, 2003 2:53 PM
Re: A Brief Introduction to Rvalue References
May 26, 2012 7:46 PM      
Consider this code:

extern void foo(A &a);
extern void foo(A &&a);

void bar(void)
{
A a,*ap = new A;

foo(*ap);
delete ap;

foo(a);
}

Would the compiler be required or allowed to use the second overload of foo() for either of the two calls to foo() in bar() ?

Separate question: was consideration given to requiring functions that take a rvalue reference to be equivalent to an explicit destructor call on the object? And, in a question related to that question, does/should the Standard require that this:

ap->~A();
delete static_cast<void *>(ap);

work?
Niels
Posts: 9 / Nickname: dekker / Registered: September 7, 2006 6:14 AM
Re: A Brief Introduction to Rvalue References
May 26, 2012 11:24 PM      
Walter Karas wrote:
> Consider this code:
>
> extern void foo(A &a);
> extern void foo(A &&a);
>
> void bar(void)
> {
> A a,*ap = new A;
>
> foo(*ap);
> delete ap;
>
> foo(a);
> }
>
> Would the compiler be required or allowed to use the
> second overload of foo() for either of the two calls to
> foo() in bar() ?

No. 'a' and '*ap' are not rvalues, so in your example, foo(A&&a) will never be called.


> Separate question: was consideration given to requiring
> functions that take a rvalue reference to be equivalent to
> an explicit destructor call on the object? And, in a
> question related to that question, does/should the
> Standard require that this:
>
> ap->~A();
> delete static_cast<void *>(ap);
>
> work?

I guess N1377 section "Alternative move designs" might be of help to you:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#Alternative%20move%20designs

Happy programming!

-- Niels
Walter
Posts: 12 / Nickname: wkaras / Registered: December 22, 2003 2:53 PM
Re: A Brief Introduction to Rvalue References
May 28, 2012 7:41 AM      
> Walter Karas wrote:
> > Consider this code:
> >
> > extern void foo(A &a);
> > extern void foo(A &&a);
> >
> > void bar(void)
> > {
> > A a,*ap = new A;
> >
> > foo(*ap);
> > delete ap;
> >
> > foo(a);
> > }
> >
> > Would the compiler be required or allowed to use the
> > second overload of foo() for either of the two calls to
> > foo() in bar() ?
>
> No. 'a' and '*ap' are not rvalues, so in your example,
> foo(A&&a) will never be called.

Could it be forced like this?

foo(static_cast<A &&>(*ap));

foo(static_cast<A &&>(a));

>
>
> > Separate question: was consideration given to
> requiring
> > functions that take a rvalue reference to be equivalent
> to
> > an explicit destructor call on the object? And, in a
> > question related to that question, does/should the
> > Standard require that this:
> >
> > ap->~A();
> > delete static_cast<void *>(ap);
> >
> > work?
>
> I guess N1377 section "Alternative move designs" might be
> of help to you:
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n13
> 77.htm#Alternative%20move%20designs
>
> Happy programming!
>
> -- Niels
Roman
Posts: 1 / Nickname: romanl / Registered: December 16, 2014 4:31 AM
Re: A Brief Introduction to Rvalue References
December 16, 2014 10:40 AM      

A a;
A&& a_ref2 = a; // an rvalue reference

This example from the article wouldn't compile with current compilers. Did things change and the article should be considered outdated?
64 posts on 5 pages.
« Previous 1 2 3 4 5 Next »