0

For below code, I want to use the std::move to improve the efficiency. I have two functions, the first function uses std::move, and the second function just calls the first function. So, do I need to use std::move again in the function "vector convertToString()"? Why and why not? Thank you.

class Entity_PortBreakMeasure
{
public:
Entity_PortBreakMeasure(){}
int portfolioId;
string portfolioName;
int businessDate;
string assetType;
string currency;
string country;
string industry;
string indicator;
double value;

inline double operator()()
{
    return value;
}

static vector<string> convertToString(Entity_PortBreakMeasure& pbm)
{

    //PORTFOLIOID   INDUSTRY    CURRENCY    COUNTRY BUSINESSDATE    ASSETTYPE   INDICATOR VALUE PORTFOLIONAME
    vector<string> result;

    result.push_back(boost::lexical_cast<string>(pbm.portfolioId));
    result.push_back(pbm.industry);
    result.push_back(pbm.currency);
    result.push_back(pbm.country);
    result.push_back(Date(pbm.businessDate).ToString());
    result.push_back(pbm.assetType);
    result.push_back(pbm.indicator);
    result.push_back(boost::lexical_cast<string>(pbm.value));
    result.push_back(pbm.portfolioName);

    return std::move(result);
}

vector<string> convertToString()
{
    return convertToString(*this);
}
Michael
  • 673
  • 2
  • 5
  • 23
  • The first function should just say `return result;`. This has been discussed many times, [e.g. here](http://stackoverflow.com/q/17473753/596781). – Kerrek SB Jul 10 '13 at 00:15

1 Answers1

0

move() shouldn't be used for either of these functions.

In the first function, you're returning a local variable. Without move(), most (all?) compilers will perform NRVO and you won't get a copy or a move -- the returned variable will be constructed directly in the returned value for the caller. Even if the compiler is, for some reason, unable to do NRVO, local variables become r-values when used as the argument to a return, so you'll get a move anyway. Using move() here serves only to inhibit NRVO and force the compiler to do a move (or a copy in the event that the move isn't viable).

In the second function, you're returning an r-value already, since the first function returns by value. move() here doesn't add anything but complexity (which might possibly confuse an optimizer into producing suboptimal code or failing to do copy elision).

Adam H. Peterson
  • 4,511
  • 20
  • 28