jeudi 28 février 2019

Is repeatedly calling move with rvalue references necessary?

struct Bar
{    
   Bar(std::string&& val)
    : m_Val(std::move(val)) {} // A

  Bar& operator=(Bar&& _other) { m_Val = std::move(_other.m_Val); }
  std::string m_Val;
}

struct Foo
{
  void Func1(Foo&& param)
  {
     Func2(std::move(param)) // B
  }

  void Func2(Foo&& param)
  {
     m_Bar = std::move(param); // C
  }

  Bar m_Bar;
};

void main()
{
   Foo f;
   std::string s = "some str";
   Bar b(std::move(s));
   f.Func1(std::move(b));
}

Give that you're calling move in main() to invoke the rvalue reference methods, is it necessary in lines A & B & C to repeat an additional call to move()? You already have the rvalue parameter, so is it doing anything different in those lines with vs without?

I understand in Bar's operator= it's necessary because you're technically moving the m_Val rather than _other itself correct?

Aucun commentaire:

Enregistrer un commentaire