0

Here is the code.

std::map<int, double>  function_xx() {
    std::map<int, double> xx_map;
    .... //some code
    return xx_map;
}

std::map<int, double>  function_yy() {
    std::map<int, double> yy_map;
    .... //some code
    return yy_map;
}

void function_abc() {
   std::map<int, double> abc_map;
   // some code
   if (match_situation_xx) {
       abc_map = std::move(function_xx());
   } else if (match_situation_yy) {
       abc_map = std::move(function_yy());
   }
   // other code
    
}

I think it does not make sense, because std::map has implemented map& operator=( map&& other ); and the return value is a right value.

Have I got it right, please?

cigien
  • 57,834
  • 11
  • 73
  • 112
Anna
  • 261
  • 1
  • 2
  • 12
  • 3
    Not really, no. The results of the function calls are already rvalues, converting them to rvalue references (which is really the only thing that `std::move` does) won't add anything useful. – Some programmer dude Nov 15 '22 at 07:16
  • 2
    Yes, it is pointless at best and potentially detrimental to performance in other cases. The purpose of `std::move` is to turn a lvalue into a rvalue, but function call expressions returning by-value (instead of by-reference) are already prvalues (a kind of rvalue). The move assignment operator will be used anyway. – user17732522 Nov 15 '22 at 07:22
  • 2
    All modern compiler will already perform Return Value Optimization (RVO) and Named Return Value Optimization (NRVO) and can generate more efficient code. So yes you are right `std::move` in this case is not useful. I am not sure on this but it might also affect compiler optimization by explicitly mentioning `std::move` here. – Daemon Nov 15 '22 at 08:59
  • Functions return rvalue and `std::move` converts lvalue into rvalue so this `std::move` is not needed. – Marek R Nov 15 '22 at 10:06

0 Answers0