The Artima Developer Community
Sponsored Link

Articles Forum
A Brief Introduction to Rvalue References

62 replies on 5 pages. Most recent reply: May 28, 2012 11:41 AM by Walter Karas

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 62 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 11: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 3: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 11: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

Flat View: This topic has 62 replies on 5 pages [ « | 2  3  4  5 ]
Topic: Coding with JRebel: Java Forever Changed Previous Topic   Next Topic Topic: Effect Choreography in Flex 4


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us