mercredi 29 août 2018

Return r-val reference reason

I see a class defined like the following:

class Foo {
 public:
  template <typename T, typename... Args>
  Foo&& addElement(Args&&... args) {       <--- why do we return Foo&&
    elements_.emplace_back(
      std::make_unique<T>(std::forward<Args>(args)...));
    return std::move(*this);
  }

 private:
  std::vector<std::unique_ptr<Bar>> elements_;
};

I can't think of any good reasons for the returning type of function addElement(). Does it make sense to return r-val reference or it's not a good idea? If so, what's the potential reason for doing that?

Aucun commentaire:

Enregistrer un commentaire