The Artima Developer Community
Sponsored Link

Articles Forum
A Brief Introduction to Rvalue References

63 replies on 5 pages. Most recent reply: Dec 16, 2014 10:40 AM by Roman L

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 63 replies on 5 pages [ « | 1 ... 2 3 4 5 ]
Walter Karas

Posts: 12
Nickname: wkaras
Registered: Dec, 2003

Re: A Brief Introduction to Rvalue References Posted: May 26, 2012 8:46 PM
Reply to this message Reply
Advertisement
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 Dekker

Posts: 9
Nickname: dekker
Registered: Sep, 2006

Re: A Brief Introduction to Rvalue References Posted: May 27, 2012 12:24 AM
Reply to this message Reply
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 Karas

Posts: 12
Nickname: wkaras
Registered: Dec, 2003

Re: A Brief Introduction to Rvalue References Posted: May 28, 2012 8:41 AM
Reply to this message Reply
> 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 L

Posts: 1
Nickname: romanl
Registered: Dec, 2014

Re: A Brief Introduction to Rvalue References Posted: Dec 16, 2014 10:40 AM
Reply to this message Reply

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?

Flat View: This topic has 63 replies on 5 pages [ « | 2  3  4  5 ]
Topic: Testing Private Methods with JUnit and SuiteRunner Previous Topic   Next Topic Topic: Borachio: Mock Objects for Scala and Android

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use