Const T & vs. T &&

I have the following function:

T foo();

And, in another function, I play with the returned value T, for example:

const T& t = foo();

or

T&& t = foo();

I want to ask:

  • Are both equivalent?

  • It is clear, what is the difference between these two approaches?

  • I know that in the first approach, the temporal value of the lifetime corresponds to the reference lifetime. Does the same thing happen when using the rvalue reference?

  • Should I even use const rvalue ( const T&&) reference ?

I use the first approach, this is what I'm used to, and it treats me well. I am still wrapping my head with rvalue and C ++ 11 links.

+5
source share
3 answers
  • No, they have different types.

  • Implementation, but hardly any difference.

  • Lifetime rvalue lvalue. , " ".

  • , . , , . , , , , .

+5
  • ( ), const, .
  • , "", .
  • .
  • , .

, move-semantics , . , , , , , .

+1

- , & &,

http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Scott-Meyers-Universal-References-in-Cpp11

, .

:

1 + 2) T

3) . : T && , wise, : t , , l.

4) The view aimed at me: how do you switch from something that is const?

Best jochen

edit: added blank lines.

+1
source

All Articles