Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++: Is std::move always invoked implicitly?

I understand that std::move can be implicitly invoked when returning a literal that can be implicitly converted to the return type of the function surrounding it. My question is are there any cases where I will have to manually use std::move. Can using rvalue references nullify this necessity?

1 Answer

Relevance
  • ?
    Lv 7
    8 years ago
    Favorite Answer

    std::move is simply a no-op conversion from lvalue to xvalue. The only time it is "invoked implicitly", that is, lvalue is treated as xvalue, is in the return statement of a function.

    Yes, there are many cases when you would want to write out "std::move". Common ones are writing move constructors, and doing things to std::unique_ptr (e.g. moving one into a vector of unique_ptrs)

    rvalue references aren't directly relevant to the idea of moving from lvalues (which is what std::move does). On the contrary, they exist to bind to rvalues, which is a whole different story.

Still have questions? Get your answers by asking now.